Table of Contents

Class SocketIONamespace

Namespace
Firesplash.GameDevAssets.SocketIOPlus
Assembly
Firesplash.GameDevAssets.SocketIOPlus.dll

This class represents a (usually connected) Socket.IO namespace and implements the EventEmitter and the EventReceiver. We try to keep our API as near to the official Socket.IO V4 API as possible within this class. https://socket.io/docs/v4/emitting-events/https://socket.io/docs/v4/listening-to-events/#eventemitter-methods

public class SocketIONamespace
Inheritance
SocketIONamespace

Fields

OnSocketIOEventThreaded

This event allows you to receive any Socket IO event (received or internally generated) without additional delay. This callback is executed in a Thread so you may not directly access unity engine functions from it! When timing is not critical, you should use the "On" method instead to register a thread safe, dispatched callback.

public DataTypes.ThreadedSocketIOEvent OnSocketIOEventThreaded

Field Value

DataTypes.ThreadedSocketIOEvent

Properties

namespacePath

public string namespacePath { get; }

Property Value

string

socketID

public string socketID { get; }

Property Value

string

state

public DataTypes.ConnectionState state { get; }

Property Value

DataTypes.ConnectionState

Methods

BugWorkaroundForReconnect()

This is a temporary workaround for a bug where namespaces are not reconnected correctly. Call this method before a reconnect to override the reconnect logic.

public void BugWorkaroundForReconnect()

Connect()

A namesapce will automatically connect unless it has manually been disconnected by calling "Disconnect()" on it. A manually disconnected namespace can be reconnected by calling this method.

public void Connect()

Disconnect()

Calling this method will disconnect the namespace and disable reconnecting to it. To reconnect, you must manually call "Connect()" on it.

public void Disconnect()

Emit(string, object[], UnityAction<object[]>)

Emits an event to the server

public void Emit(string eventName, object[] payloads = null, UnityAction<object[]> acknowledgementCallback = null)

Parameters

eventName string

The name of the emitted event

payloads object[]

An optional array of payload objects (Any objects supported by Json.Net OR byte[]). Every array element is transmitted as an individual payload. An array of three strings is the equivalent to JS io.emit("someEvent", "string1", "string2", "string3")

acknowledgementCallback UnityAction<object[]>

An optional callback. If provided, the emit will be an acknowledgement. This requires a payload.

Emit<T>(string, T, UnityAction<object[]>)

Emits an event to the server that has only one payload (for example a string, byte[] or a - through Json.Net - serializable object)

public void Emit<T>(string eventName, T payload, UnityAction<object[]> acknowledgementCallback = null)

Parameters

eventName string

The name of the emitted event

payload T

The payload

acknowledgementCallback UnityAction<object[]>

An optional callback. If provided, the emit will be an acknowledgement. This requires a payload. The callback received an object[] where every elemtn is eighter a byte[] or a JToken depending on what the server sent.

Type Parameters

T

The type of the primitive payload

Off(string, UnityAction<SocketIOEvent>)

Unregisters a callback for a specific event.

public bool Off(string eventName, UnityAction<SocketIOEvent> callback)

Parameters

eventName string

The name of the event

callback UnityAction<SocketIOEvent>

The callback which should be removed

Returns

bool

True if the callback was removed, false otherwise

OffAny()

Unregisters all callbacks for the catch-all event.

public void OffAny()

OffAny(UnityAction<SocketIOEvent>)

Unregisters a callback for the catch-all event.

public bool OffAny(UnityAction<SocketIOEvent> callback)

Parameters

callback UnityAction<SocketIOEvent>

The callback which should be removed

Returns

bool

True if the callback was removed, false otherwise

On(string, UnityAction)

Registers a callback for a specific event that delivers NO payload. It will NOT invoke if a payload is contained in the received message! For any more advanced payload handling, use the "On" method without type assignment. Warning: You can not unregister this listener using "Off"! The callback is dispatched, so it will always call from the main thread and you can safely access Unity functions from it!

public void On(string eventName, UnityAction callback)

Parameters

eventName string

The EventName to subscribe to

callback UnityAction

The Callback to invoke on receiption

On(string, UnityAction<SocketIOEvent>)

Registers a callback for a specific event. The callback is dispatched, so it will always call from the main thread and you can safely access Unity functions from it!

public void On(string eventName, UnityAction<SocketIOEvent> callback)

Parameters

eventName string

The EventName to subscribe to

callback UnityAction<SocketIOEvent>

The Callback to invoke on receiption

OnAny(UnityAction<SocketIOEvent>)

Registers a callback for ANY event (catch-all) The callback is dispatched, so it will always call from the main thread and you can safely access Unity functions from it!

public void OnAny(UnityAction<SocketIOEvent> callback)

Parameters

callback UnityAction<SocketIOEvent>

The Callback to invoke on receiption of any event

OnAny<T>(UnityAction<string, T>)

public void OnAny<T>(UnityAction<string, T> callback)

Parameters

callback UnityAction<string, T>

Type Parameters

T

On<T>(string, UnityAction<T>)

This is a wrapper included for convenience in simple projects. It has some limitations. Registers a callback for a specific event which only has ONE payload of a GIVEN TYPE. If the received event has more than one payload, the additional payloads will be ignored. If the first payload is not of the correct type, the callback will not fire. For any more advanced payload handling, use the "On" method without type assignment. Warning: You can not unregister this listener using "Off"! The callback is dispatched, so it will always call from the main thread and you can safely access Unity functions from it!

public void On<T>(string eventName, UnityAction<T> callback)

Parameters

eventName string

The EventName to subscribe to

callback UnityAction<T>

The Callback to invoke on receiption

Type Parameters

T

The expected type of the first payload (JObject, JArray or a primitive type supported by JValue)

See Also
JValue

Once(string, UnityAction<SocketIOEvent>)

Registers a callback for a specific event which is only called once and then destroyed. The callback is dispatched, so it will always call from the main thread and you can safely access Unity functions from it! Once-Callbacks are called before registered permanent handlers

public void Once(string eventName, UnityAction<SocketIOEvent> callback)

Parameters

eventName string

The EventName to subscribe to

callback UnityAction<SocketIOEvent>

The Callback to invoke ONCE on receiption

Once<T>(string, UnityAction<T>)

This is a wrapper included for convenience in simple projects. It has some limitations. Registers a callback for a specific event which only has ONE payload of a GIVEN TYPE. This callback will only fire the first time, this event is received after registering the ccallback. If the received event has more than one payload, the additional payloads will be ignored. If the first payload is not of the correct type, the callback will not fire. If the event is received and the payloads are not compatible, the callback is still removed from the list. For any more advanced payload handling, use the "Once" method without type assignment. The callback is dispatched, so it will always call from the main thread and you can safely access Unity functions from it!

public void Once<T>(string eventName, UnityAction<T> callback)

Parameters

eventName string

The EventName to subscribe to

callback UnityAction<T>

The Callback to invoke on receiption

Type Parameters

T

The expected type of the first payload (JObject, JArray or a primitive type supported by JValue)

See Also
JValue

RemoveAllListeners()

Unregisters all callbacks (once and permanent) for all events - including catchall.

public void RemoveAllListeners()

RemoveAllListeners(string)

Unregisters all callbacks (once and permanent) for a specific event.

public void RemoveAllListeners(string eventName)

Parameters

eventName string

The name of the event