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
| Type | Value | Description |
|---|---|---|
| Opened | 1 | The WebSocket is opened and ready to send and receive messages. |
| Closed | 2 | The WebSocket is closed. |
| Error | 3 | An error has occurred. |
| MsgSent | 4 | A message has been sent and has left the sending queue. |
| TextReceived | 5 | A WebSocket text message has been received. |
| DataReceived | 6 | A WebSocket binary message has been received. |
| PingReceived | 7 | A WebSocket Ping message has been received. |
| PongReceived | 8 | A WebSocket Pong message has been received. |
| Timer | 9 | A 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)
| Name | Type | Description |
|---|---|---|
| Protocol | String | The protocol selected by the WebSocket server during opening. |
| TargetIPAddr | String | The IP address of the connected WebSocket server. |
| EffectiveUrl | String | The effective URL after all redirections. |
Closed event (type: 2)
| Name | Type | Description |
|---|---|---|
| Code | Int | The WebSocket status code. |
| Reason | String | The reason why the WebSocket was closed. |
| Error | Int | Internal error code. A non-zero value indicates that the WebSocket was closed because of an error. |
| ErrorMsg | String | A brief error description, which is useful for debugging. |
Error event (type: 3)
| Name | Type | Description |
|---|---|---|
| Error | Int | Internal error code. A non-zero value indicates that the WebSocket was closed because of an error. |
| ErrorMsg | String | A brief error description, which is useful for debugging. |
MsgSent event (type: 4)
| Name | Type | Description |
|---|---|---|
| Opcode | Int | The WebSocket opcode of the sent message. |
| Msg | Int | The message ID, which is a unique identifier assigned when the message was created. It provides tracking when a specific message has been sent. |
| Size | Int | The size of the message sent. |
TextReceived event (type: 5)
| Name | Type | Description |
|---|---|---|
| Text | String | The text data of the text message received. |
DataReceived event (type: 6)
| Name | Type | Description |
|---|---|---|
| Data | ByteArray | The binary data of the binary message received. |
PingReceived event (type: 7)
| Name | Type | Description |
|---|---|---|
| Text | String | The text data of the received Ping message (if valid). |
| Data | ByteArray | The binary data of the received Ping message (if valid). |
PongReceived event (type: 8)
| Name | Type | Description |
|---|---|---|
| Text | String | The text data of the received Pong message (if valid). |
| Data | ByteArray | The binary data of the received Pong message (if valid). |
Timer event (type: 9)
| Name | Type | Description |
|---|---|---|
| TimerId | String | The timer ID string assigned by the roWebSocket.SetTimer() method. |
| Occur | Int | The 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 subUpdated 2 days ago