roWebSocketEvent

Asynchronous event notifications generated by an roWebSocket connection

During an open WebSocket connection, the roWebSocket object generates multiple asynchronous WebSocket events that are delivered as roWebSocketEvent objects via the object's message port. The roWebSocket sends the roWebSocketEvent with the following predicates that indicate its valid event types:

  • GetType() as Int
  • GetSocketId() as Int
  • GetSocketData() as Dynamic
  • GetInfo() as Object

GetType() as Int

Description

Returns the event type of the WebSocket event.

Event types

TypeValueDescription
Opened1The WebSocket is opened and ready to send and receive messages.
Closed2The WebSocket is closed.
Error3An error has occurred.
MsgSent4A message has been sent and has left the sending queue.
TextReceived5A WebSocket text message has been received.
DataReceived6A WebSocket binary message has been received.
PingReceived7A WebSocket Ping message has been received.
PongReceived8A WebSocket Pong message has been received.
Timer9A timer event has occurred.

Return Value

The event type value.

Example

sub Setup()
'Sets event constants like: m.OpenEvent = 1, m.ClosedEvent = 2, …
SetWSEventConstants(m)
end sub

sub CreateWebSocket(url as String)
	m.websocket = createObject("roWebSocket")
	m.port = createObject("roMessagePort")
    m.websocket.setMessagePort(m.port)
    ' Set Url
    m.websocket.SetUrl(url)
	print "Opening connection..."
    m.websocket.Open()
end sub

sub HandleWSEvent(msg as Object)
	if msg.GetType()= m.OpenEvent ' = 1
		print "WebSocket opened."
		' Sending a text message after opening.
		m.websocket.Send("Client ready")
	end if
end sub

GetSocketId() as Int

Description

Returns a unique number for the WebSocket instance that originated the event.

Return Value

The WebSocket identifier.

Example

sub Setup()
	m.webSockets = {} ' Creating pool of WebSockets
 	 m.port = createObject("roMessagePort")
	'Sets event constants like: m.OpenEvent = 1, m.ClosedEvent = 2, …
	SetWSEventConstants(m)
end sub

sub CreateWebSocket(url as String)
	websocket = createObject("roWebSocket")
    websocket.setMessagePort(m.port)
    ' Set Url
    websocket.SetUrl(m.top.testUrl)
    ' Add socket to the pool using socket id string value as a key
    m.webSockets.AddReplace(websocket.GetSocketId().toStr(), websocket)
	print "Opening connection..."
    websocket.Open()
end sub

sub HandleWSEvent(msg as Object)
event = msg.GetType()
	if event = m.CloseEvent ' = 2
		socket_id = msg.GetSocketId().ToStr()
		m.webSockets.Delete(socket_id)
	end if
end sub

GetSocketData() as Dynamic

Description

Returns the socket data object stored in the WebSocket instance that originated the event. The socket data object is set with the roWebSocket.SetData() method.

Return Value

The socket data object.

Example

websocket = createObject("roWebSocket")
websocket.setMessagePort(m.port)
' Set Url
websocket.SetUrl(ws_url)
' Socket data - it will be available in WebSocket events via GetSocketData()
' The socket data can be any Object type.
' Setting associative array in socket data.
data = {
     Data1 : 239,
     Data2 : "DataValue2"
}
websocket.SetData(data)
' Setting integer value in socket data.
websocket.SetData(239)
' Setting string value in socket data.
websocket.SetData("Some text")
' "Tracing" function taking event object and printing the socket data.
sub TraceSocketData(msg as Object)
    data = msg.GetSocketData()
    if data <> invalid
        print "Socket data: "; data
    end if
end sub

GetInfo() as Object

Description

Returns an associative array with event-specific information.

Return Value

The event-info associative array.

Event-specific fields

Opened event (type: 1)
NameTypeDescription
ProtocolStringThe protocol selected by the WebSocket server during opening.
TargetIPAddrStringThe IP address of the connected WebSocket server.
EffectiveUrlStringThe effective URL after all redirections.
Closed event (type: 2)
NameTypeDescription
CodeIntThe WebSocket status code.
ReasonStringThe reason why the WebSocket was closed.
ErrorIntInternal error code. A non-zero value indicates that the WebSocket was closed because of an error.
ErrorMsgStringA brief error description, which is useful for debugging.
Error event (type: 3)
NameTypeDescription
ErrorIntInternal error code. A non-zero value indicates that the WebSocket was closed because of an error.
ErrorMsgStringA brief error description, which is useful for debugging.
MsgSent event (type: 4)
NameTypeDescription
OpcodeIntThe WebSocket opcode of the sent message.
MsgIntThe message ID, which is a unique identifier assigned when the message was created. It provides tracking when a specific message has been sent.
SizeIntThe size of the message sent.
TextReceived event (type: 5)
NameTypeDescription
TextStringThe text data of the text message received.
DataReceived event (type: 6)
NameTypeDescription
DataByteArrayThe binary data of the binary message received.
PingReceived event (type: 7)
NameTypeDescription
TextStringThe text data of the received Ping message (if valid).
DataByteArrayThe binary data of the received Ping message (if valid).
PongReceived event (type: 8)
NameTypeDescription
TextStringThe text data of the received Pong message (if valid).
DataByteArrayThe binary data of the received Pong message (if valid).
Timer event (type: 9)
NameTypeDescription
TimerIdStringThe timer ID string assigned by the roWebSocket.SetTimer() method.
OccurIntThe timer occurrence, which indicates how many times the timer event has fired since being set up.

Example

sub Setup()
'Sets event constants like: m.OpenEvent = 1, m.ClosedEvent = 2, …
SetWSEventConstants(m)
end sub

sub CreateWebSocket(url as String)
m.websocket = createObject("roWebSocket")
m.port = createObject("roMessagePort")
    m.websocket.setMessagePort(m.port)
    ' Set Url
    m.websocket.SetUrl(url)
  
print "Opening connection..."
    m.websocket.Open()
end sub

sub TraceOpenEvent(msg as Object)
    ' Open info
    info = msg.GetInfo()
    print "Protocol="; info.Protocol
    print "TargetIPaddr="; info.targetIPAddr
    print "effectiveUrl="; info.effectiveUrl
    ' Socket Data
    TraceSocketData(msg)
end sub

sub TraceCloseEvent(msg as Object)
    ' Close info
    info = msg.GetInfo()    
    print "code="; StrI(info.Code); ", reason="; info.Reason
    print "error="; StrI(info.Error); ", err_msg="; info.ErrorMsg
    ' Socket Data
    TraceSocketData(msg)
end sub

sub TraceSocketData(msg as Object)
    data = msg.GetSocketData()
    if data <> invalid
        print "Socket data: "; data
    end if
end sub

sub HandleWSEvent(msg as Object)
	if msg.GetType()= m.OpenEvent ' = 1
		print "WebSocket opened."
		' Tracing Opened event information.
		TraceOpenEvent(msg)
		' Sending a text message after opening.
		m.websocket.Send("Client ready")
	else if msg.GetType()= m.ClosedEvent ' = 2
		print "WebSocket closed."
		' Tracing Closed event information.
		TraceCloseEvent(msg)
	end if
end sub

Did this page help you?