sends data between two network endpoints More...
Public Member Functions | |
NetConnection (object socket) | |
create a new net connection from a socket More... | |
close (boolean kill) | |
close a connection The remote endpoint of the connection will get an onClose() callback. More... | |
NetConnection | onClose (function callback) |
set callback to handle connection closing More... | |
NetConnection | onMessage (function callback) |
set callback to handle receipt of a network message More... | |
send (string message) | |
send a string to the remote endpoint More... | |
send (MemBlock message) | |
send a block of memory to the remote endpoint More... | |
send (ISerializable message) | |
send a serializable object to the remote endpoint More... | |
send (object message) | |
send a JavaScript object to the remote endpoint More... | |
sendDgram (string message) | |
send a string to the remote endpoint via UDP More... | |
sendDgram (MemBlock message) | |
send a block of memory to the remote endpoint via UDP More... | |
sendDgram (ISerializable message) | |
send a serializable object to the remote endpoint via UDP More... | |
sendDgram (object message) | |
send a JavaScript object to the remote endpoint via UDP More... | |
Public Attributes | |
boolean | hasDgram |
true if the connection supports UDP/Datagram communications More... | |
string | localAddr |
the ip address of the local end of the connection More... | |
number | localPort |
the port that the local end of the connection is communicating over More... | |
string | remoteAddr |
the ip address of the remote end of the connection More... | |
number | remotePort |
the port that the remote end of the connection is communicating over More... | |
sends data between two network endpoints
NetConnection is part of the PDG Engine's custom protocol for games. It handles protocol negotiation. It manages communication over both a TCP connection (reliable transport with guaranteed delivery) and UDP communications (small and unreliable but fast) with the server.
NetConnection only represents an established connection created by NetClient or NetServer.
NetConnection | ( | object | socket | ) |
create a new net connection from a socket
socket | a Node.js net.Socket object |
close | ( | boolean | kill | ) |
close a connection The remote endpoint of the connection will get an onClose() callback.
close a connection
kill | true if the connection should be abruptly dropped rather than cleanly disconnected. See AlsoNetConnection.onClose |
The remote endpoint of the connection will get their onClose() callback.
kill | true if the connection should be abruptly dropped rather than cleanly disconnected. |
onClose | ( | function | callback | ) |
set callback to handle connection closing
Whenever a network connection is dropped by the remote end (whether cleanly or abrubtly), you will get an onClose callback to the function specified.
callback | the function that should be called |
onMessage | ( | function | callback | ) |
set callback to handle receipt of a network message
Whenever the remote end sends a message, you will get an onMessage callback to the function specified with the message that was sent.
callback | the function that should be called when a message is received. The message is the first parameter passed to the callback function. Second parameter is the connection object. Third parameter is a string with the transport type (currently either 'upd' or 'tcp', where 'udp' means it was sent via sendDgram() and 'tcp' means it was sent via send() ) |
send | ( | string | message | ) |
send a string to the remote endpoint
The string is sent guaranteed delivery (TCP), and will arrive in the order sent with other messages.
NetConnection handles packet framing for you, so the NetConnection on the remote endpoint will receive an onMessage callback with the string you sent. If the message cannot be sent, you will eventually get an onClose() callback to indicate that the connection has been lost.
message | the string to send |
send | ( | MemBlock | message | ) |
send a block of memory to the remote endpoint
The block is sent guaranteed delivery (TCP), and will arrive in the order sent with other messages.
NetConnection handles packet framing for you, so the NetConnection on the remote endpoint will receive an onMessage callback with the string you sent. If the message cannot be sent, you will eventually get an onClose() callback to indicate that the connection has been lost.
message | the MemBlock to send |
send | ( | ISerializable | message | ) |
send a serializable object to the remote endpoint
The object is sent guaranteed delivery (TCP), and will arrive in the order sent with other messages.
NetConnection handles packet framing for you, so the NetConnection on the remote endpoint will receive an onMessage callback with the object you sent. If the message cannot be sent, you will eventually get an onClose() callback to indicate that the connection has been lost.
message | the serializable object to send |
send | ( | object | message | ) |
send a JavaScript object to the remote endpoint
The object is sent guaranteed delivery (TCP), and will arrive in the order sent with other messages.
NetConnection handles packet framing for you, so the NetConnection on the remote endpoint will receive an onMessage callback with the object you sent. If the message cannot be sent, you will eventually get an onClose() callback to indicate that the connection has been lost.
message | the object to send |
sendDgram | ( | string | message | ) |
send a string to the remote endpoint via UDP
The string will be sent using UDP if it is available. Delivery is not guaranteed and neither is in-order deliver of messages. This is ideal for small bits of information that are frequently resent, such as position or status updates.
NetConnection handles packet framing for you, so if the packet arrives, the NetConnection on the remote endpoint will receive an onMessage callback with the string you sent. If the message cannot be sent you will not get any notification. It will simply disappear into the ether.
message | the string to send |
sendDgram | ( | MemBlock | message | ) |
send a block of memory to the remote endpoint via UDP
The block will be sent using UDP if it is available. Delivery is not guaranteed and neither is in-order deliver of messages. This is ideal for small bits of information that are frequently resent, such as position or status updates.
NetConnection handles packet framing for you, so if the packet arrives, the NetConnection on the remote endpoint will receive an onMessage callback with the object you sent. If the message cannot be sent you will not get any notification. It will simply disappear into the ether.
message | the MemBlock to send |
sendDgram | ( | ISerializable | message | ) |
send a serializable object to the remote endpoint via UDP
The object will be sent using UDP if it is available. Delivery is not guaranteed and neither is in-order deliver of messages. This is ideal for small bits of information that are frequently resent, such as position or status updates.
NetConnection handles packet framing for you, so if the packet arrives, the NetConnection on the remote endpoint will receive an onMessage callback with the object you sent. If the message cannot be sent you will not get any notification. It will simply disappear into the ether.
message | the serializable object to send |
sendDgram | ( | object | message | ) |
send a JavaScript object to the remote endpoint via UDP
The object will be sent using UDP if it is available. Delivery is not guaranteed and neither is in-order deliver of messages. This is ideal for small bits of information that are frequently resent, such as position or status updates.
NetConnection handles packet framing for you, so if the packet arrives, the NetConnection on the remote endpoint will receive an onMessage callback with the string you sent. If the message cannot be sent you will not get any notification. It will simply disappear into the ether.
message | the object to send |
hasDgram |
true if the connection supports UDP/Datagram communications
localAddr |
the ip address of the local end of the connection
localPort |
the port that the local end of the connection is communicating over
remoteAddr |
the ip address of the remote end of the connection
remotePort |
the port that the remote end of the connection is communicating over