Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS MacCF Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCF/CFSocket Get Example
This example is the version from Sun, 17th Mar 2012.
Project "CFSocket Get Example.xojo_binary_project"
Class MainWindow Inherits Window
Control iURL Inherits TextField
ControlInstance iURL Inherits TextField
End Control
Control GetButton Inherits PushButton
ControlInstance GetButton Inherits PushButton
EventHandler Sub Action()
stream=new CFReadStream
stream.fetchUrl iURL.text
End EventHandler
End Control
Control result Inherits TextArea
ControlInstance result Inherits TextArea
End Control
Control Status Inherits Label
ControlInstance Status Inherits Label
End Control
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Property Protected stream As cfreadStream
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class CFReadStream Inherits CFReadStreamMBS
EventHandler Sub Callback(reason as integer)
// constants for the reason:
const kCFStreamEventNone = 0
const kCFStreamEventOpenCompleted = 1
const kCFStreamEventHasBytesAvailable = 2
const kCFStreamEventCanAcceptBytes = 4
const kCFStreamEventErrorOccurred = 8
const kCFStreamEventEndEncountered = 16
Select case reason
case kCFStreamEventOpenCompleted
eventOpenCompleted
case kCFStreamEventHasBytesAvailable
eventHasBytesAvailable
case kCFStreamEventCanAcceptBytes
eventCanAcceptBytes
case kCFStreamEventErrorOccurred
eventErrorOccurred
case kCFStreamEventEndEncountered
eventEndEncountered
end Select
End EventHandler
Sub Destructor()
if events then
me.RemoveEvents
events=false
end if
End Sub
Sub eventCanAcceptBytes()
System.DebugLog "CanAcceptBytes Event"
End Sub
Sub eventEndEncountered()
System.DebugLog "EndEncountered Event"
MainWindow.Status.text="Fetch is complete."
End Sub
Sub eventErrorOccurred()
System.DebugLog "ErrorOccurred Event"
MainWindow.Status.text="Error occurred."
End Sub
Sub eventHasBytesAvailable()
dim s as String
System.DebugLog "HasBytesAvailable Event"
s=me.ReadString(2048)
if s<>"" then
// Append text
MainWindow.result.SelStart=len(MainWindow.result.text)
MainWindow.result.SelLength=0
MainWindow.result.seltext=s
end if
End Sub
Sub eventOpenCompleted()
System.DebugLog "OpenCompleted Event"
MainWindow.Status.text="Stream is open."
End Sub
Sub fetchUrl(urlstring as string)
dim url as String
url=urlstring
if len(url)<1 then
Return
end if
if left(url,7)<>"http://" and left(url,8)<>"https://" then
url="http://"+url
end if
cfurl=NewCFURLMBSStr(url,nil)
cfget=NewCFStringMBS("GET")
cfhttp=me.kCFHTTPVersion1_1
request=CFHTTPMessageCreateRequestMBS(cfget,cfurl,cfhttp)
// start the fetch
if me.CreateForHTTPRequest(request) then
if not events then // Install Event handler if not already installed
me.InstallEvents
events=true
end if
if me.Open then
MainWindow.Status.text="Downloading..."
else
MainWindow.Status.text="Failed to open stream."
end if
else
MainWindow.Status.text="Creating the stream failed."
end if
Exception
End Sub
Property Protected cfget As cfstringMBS
Property Protected cfhttp As CFStringMBS
Property Protected cfstring As CFStringMBS
Property Protected cfurl As CFURLMBS
Property Protected events As boolean
Property Protected request As CFHTTPMessageMBS
End Class
Class App Inherits Application
End Class
End Project
The items on this page are in the following plugins: MBS MacCF Plugin.