Sending Events¶
Start sending events only after a connect event!
Sending an event can be done with the Emit
function. Its first parameter is the name of the event, followed by an optional list of parameters. A parameter can be any value with a simple type (like int
, float
, char
, string
, etc.) or complex object (like Dictionary<>
, user defined classes and struct), until the parser of the SocketManager
can encode/decode it.
When the Socket's Emit is called, the event name and all of its parameters if there's any, gets encoded by the SocketManager
's parser and sent over to the server on the current transport.
Example
Send message to the server | |
---|---|
Subscription and event handling on the server-side | |
---|---|
On the server the message gets decoded and handled by the subscription. In this example, the msg
variable will contain the "msg 1"
string.
To avoid any hard to catch issues, the number of parameters should match on both client and server side!
Examples¶
Here are some other examples.
Example
In this example, only the event will be sent, without any parameters.
Send event without any parameter | |
---|---|
Example
In this example, the client is sending two parameters with the event. The first one is a number and the second one is an object.
Send multiple parameters | |
---|---|
Example
This example is the same as the previous one, but instead of a concrete class(NewMessageData
) it uses an anonymous type for a more dynamic, javascript-ish feeling.
Send multiple parameters | |
---|---|
Volatile Events¶
A volatile event won't sent when the client can't send it right there. When no transport is ready to send a volatile event, instead of buffering to send later, it will be discarded. An event can be marked as volatile by calling Volatile()
on the socket first: