-
Notifications
You must be signed in to change notification settings - Fork 7
Handle Socket
ClusterWS Swift Client provides an easy interface to communicate with ClusterWS server.
Please note that send
and on
events are used only for server
<-> client
communication, you won't be able to communicate with another user across different CPU and Machines. To communicate with another user across CPU and Machines use Pub/Sub system. (check client
<-> client
communication guide for more information)
To listen on events which are send from server you should use on
method:
/**
event: String - can be any string you wish
data: Any - is what you send from the client
*/
webSocket.on(event: "my-event-name") { (data) in
// your code to execute on event
}
ClusterWS delegate methods
// called when WebSocket become open / connected
func onConnect() {}
// called on WebSocket disconnect / close event
func onDisconnect(code: Int?, reason: String?) {}
// called when WebSocket is error out
func onError(error: Error) {}
Don't forget to assign class delegate to listen for those events
webSocket.delegate = self
To send message to the server you should use send
method:
/**
event: String - can be any string you wish (client must listen on this event name)
data: Any - is what you want to send to the client
*/
webSocket.send(event: "my-event-name", data: "any-data-you-want")
Try to do not send events which starts with #
or reserved disconnect
, connection
, error
events.
We would really appreciate if you give us stars (on all our repositories) this will motivate us to work harder. Thank you very much.