'Class module
Sub Class_Globals
Private callback As Object
Private event As String
Dim WS_SOCKET As Socket
Dim WS_IO_ASTREAM As AsyncStreams
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(callBackModule As Object,callBackEvent As String)
callback = callBackModule
event = callBackEvent
WS_SOCKET.Initialize("ws_socket")
End Sub
Sub ws_socket_Connected(Connected As Boolean)As Boolean
Try
If Connected = True Then
If Not(WS_IO_ASTREAM.IsInitialized ) Then
WS_IO_ASTREAM.Initialize(WS_SOCKET.InputStream,WS_SOCKET.OutputStream,"wsStream")
End If
Else
If SubExists(callback, event & "_failure") Then
CallSub2(callback, event & "_failure",LastException.Message)
End If
End If
If SubExists(callback, event & "_Connected") Then
CallSub2(callback, event & "_Connected",Connected)
End If
Catch
If SubExists(callback, event & "_socket_failure") Then
CallSub2(callback, event & "_failure",LastException.Message)
End If
End Try
Return Connected
End Sub
Sub wsStream_NewData (Buffer() As Byte)
Dim wsData As String
wsData = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
If SubExists(callback, event & "_DataArrival") Then
CallSub2(callback, event & "_DataArrival",wsData)
End If
End Sub
Sub wsStream_Terminated()
ws_close
End Sub
Sub wsStream_Error
If SubExists(callback, event & "_StreamError") Then
CallSub2(callback, event & "_StreamError", LastException.Message)
End If
End Sub
Sub ws_connect(pHost As String, pPort As Int, pTimeout As Int)
If Not(WS_SOCKET.IsInitialized) Then
WS_SOCKET.Initialize("ws_socket")
End If
WS_SOCKET.Connect(pHost,pPort,pTimeout)
End Sub
Sub ws_close()
'If WS_SOCKET.Connected Then
WS_IO_ASTREAM.Close
WS_SOCKET.Close
'End If
If SubExists(callback, event & "_closed") Then
CallSub(callback, event & "_closed")
End If
End Sub
Sub ws_isConnected() As Boolean
Return WS_SOCKET.Connected
End Sub
Sub ws_isInitialised() As Boolean
Return WS_SOCKET.IsInitialized
End Sub
Sub ws_senddata(pData As String)
Dim Buffer() As Byte
If Not(ws_isConnected) Then
Log(DateTime.Time(DateTime.Now) & " - ws_senddata() No connection ")
Return
End If
Try
If Not(WS_IO_ASTREAM.IsInitialized) Then
WS_IO_ASTREAM.Initialize(WS_SOCKET.InputStream,WS_SOCKET.OutputStream,"wsStream")
End If
' // wrap the packet
pData = Main.tx.stx & pData & Main.tx.eot
' // load byte array
Buffer = pData.GetBytes("UTF8")
If Not(WS_IO_ASTREAM.Write(Buffer)) Then
If SubExists(callback, event & "_failure") Then
CallSub2(callback, event & "_failure",LastException.Message)
End If
End If
Catch
' // reset the connection
If SubExists(callback, event & "_StreamError") Then
CallSub2(callback, event & "_StreamError", "ws_senddata() " & LastException.Message)
End If
End Try
End Sub