Class: PhotonPeer

Photon.PhotonPeer(protocol, address, subprotocolopt, debugNameopt)

Instances of the PhotonPeer class are used to connect to a Photon server and communicate with it. A PhotonPeer instance allows communication with the Photon Server, which in turn distributes messages to other PhotonPeer clients. An application can use more than one PhotonPeer instance, which are treated as separate users on the server. Each should have its own listener instance, to separate the operations, callbacks and events.

Constructor

new PhotonPeer(protocol, address, subprotocolopt, debugNameopt)

Parameters:
Name Type Attributes Default Description
protocol Photon.ConnectionProtocol Connection protocol.
address string Server address:port.
subprotocol string <optional>
"" WebSocket protocol.
debugName string <optional>
"" Log messages prefixed with this value.

Members

keepAliveTimeoutMs :number

Peer sends 'keep alive' message to server as this timeout exceeded after last send operation. Set it < 1000 to disable 'keep alive' operation

Type:
  • number
Default Value:
  • 3000

(static, readonly) StatusCodes

Enum for peer status codes. Use to subscribe to status changes.

Properties:
Name Type Description
connecting string Is connecting to server.
connect string Connected to server.
connectFailed string Connection to server failed.
disconnect string Disconnected from server.
connectClosed string Connection closed by server.
error string General connection error.
timeout string Disconnected from server for timeout.

Methods

addEventListener(eventCode, callback)

Registers listener for custom event.

Parameters:
Name Type Description
eventCode number Custom event code.
callback function The listener function that processes the event. This function may accept object with event content.

addPeerStatusListener(statusCode, callback)

Registers listener for peer status change.

Parameters:
Name Type Description
statusCode PhotonPeer.StatusCodes Status change to this value will be listening.
callback function The listener function that processes the status change. This function don't accept any parameters.

addResponseListener(operationCode, callback)

Registers listener for operation response.

Parameters:
Name Type Description
operationCode number Operation code.
callback function The listener function that processes the event. This function may accept object with operation response content.

connect()

Starts connection to server.

disconnect()

Disconnects from server.

isClosing() → {boolean}

Checks if peer is closing.

Returns:
True if peer is closing.
Type
boolean

isConnected() → {boolean}

Checks if peer is connected.

Returns:
True if peer is connected.
Type
boolean

isConnecting() → {boolean}

Checks if peer is connecting.

Returns:
True if peer is connecting.
Type
boolean

onUnhandledEvent(eventCode, argsopt)

Called if no listener found for received custom event. Override to relay unknown event to user's code or handle known events without listener registration.

Parameters:
Name Type Attributes Description
eventCode number Code of received event.
args object <optional>
Content of received event or empty object.

onUnhandledEvent(operationCode, argsopt)

Called if no listener found for received operation response event. Override to relay unknown response to user's code or handle known responses without listener registration.

Parameters:
Name Type Attributes Description
operationCode number Code of received response.
args object <optional>
Content of received response or empty object.

removeEventListener(eventCode, callback)

Removes listener if exists for custom event.

Parameters:
Name Type Description
eventCode number Event code to remove to remove listener for.
callback function Listener to remove.

removeEventListenersForCode(eventCode)

Removes all listeners for custom event specified.

Parameters:
Name Type Description
eventCode number Event code to remove all listeners for.

removePeerStatusListener(statusCode, callback)

Removes listener if exists for peer status change.

Parameters:
Name Type Description
statusCode string One of PhotonPeer.StatusCodes to remove listener for.
callback function Listener to remove.

removePeerStatusListenersForCode(statusCode)

Removes all listeners for peer status change specified.

Parameters:
Name Type Description
statusCode string One of PhotonPeer.StatusCodes to remove all listeners for.

removeResponseListener(operationCode, callback)

Removes listener if exists for operation response.

Parameters:
Name Type Description
operationCode number Operation code to remove listener for.
callback function Listener to remove.

removeResponseListenersForCode(operationCode)

Removes all listeners for operation response specified.

Parameters:
Name Type Description
operationCode number Operation code to remove all listeners for.

sendOperation(code, dataopt, sendReliableopt, channelIdopt)

Sends operation to the Photon Server.

Parameters:
Name Type Attributes Default Description
code number Code of operation.
data object <optional>
Parameters of operation as a flattened array of key-value pairs: [key1, value1, key2, value2...]
sendReliable boolean <optional>
false Selects if the operation must be acknowledged or not. If false, the operation is not guaranteed to reach the server.
channelId number <optional>
0 The channel in which this operation should be sent.

setLogLevel(level)

Sets peer logger level.

Parameters:
Name Type Description
level Photon.Logger.Level Logging level.