Connects clients and servers with UDP.
When connecting, is uses handshaking, and when connected, it uses PING packets to discover closed connections. When the connection is stalled, it can queue the messages, which are sent out after the connection is reestablished.
You can use a subclass instance and implement Callbacks to react on new packets.
Client side has only one connection (to the server), but the server side can send messages to any or all of the connected clients. To send a message to all connected clients, use UDPIPE.sendAll. To send a message to the specified client, you need to call UDPIPE.send with a client’s socket as an argument.
See also: UDPIPE Description
Here are the simplest client and a server. You need a lua with luasockets to run the examples.
UDPIPE=require 'UDPIPE' client=UDPIPE() host, port='127.0.0.1', 1234 client:connect(host, port) function client:onConnect(sock) self:send("HelloWorld") end function client:onNewMessage(msg) print('Got message:', msg) end while true do client:step() end
UDPIPE=require 'UDPIPE' server=UDPIPE() host, port='127.0.0.1', 1234 server:start(port, host) function server:onNewMessage(msg, sock) self:send('Reply from server: '..msg, sock) end while true do server:step() end
UDPIPE | Connects clients and servers with UDP. |
Private functions | |
_startUdp | Starts listening for UDP packets |
Public methods | |
constructor | Creates new UDPIPE instance. |
setTimeout | Sets the timeout for all current and newly opened sockets. |
setDebug | Sets the debugging. |
setToken | Sets the token for handshaking. |
start | Starts the server. |
sendAll | Sends a message to all connected sockets (clients). |
send | Sends a message to a connected socket. |
getSentPackets | Returns the number of packets sent out. |
getReceivedPackets | Returns the number of packets received. |
ping | Sends a ping message to server (in client mode) or all connected sockets (in server mode). |
step | Keeps the client/server running. |
close | Close the connection. |
connect | Connects as a client to the server. |
Private methods | |
_onTimeout | Called when no packet is received in some amount of time. |
_onNewClient | Callback called when new client connection is detected. |
_setPeer | Sets the peer for the socket, makes socket connected. |
_unsetPeer | Unsets the peer for the socket, makes socket unconnected. |
_onNewMessage | Called when new packed arrives on connected socket. |
Callbacks | |
onNewClient | Called when new client is connected. |
onNewMessage | Called when new message arrives. |
onDisconnect | Called when a socket is disconnected. |
onConnect | Called when client connected to the Server, or a new client has connected. |
onClose | Called when client or server closed the connection. |
onConnecting | Called when client is connecting to the Server on a final port. |
onStalled | Called when the connection lags. |
onUnstalled | Connection is no longer stalled. |
onMaxClients | Called when too much client connections is open. |
onPong | Called when PONG packet is received. |
onPing | Called when PING packet is received. |
onBadToken | Called when bad token is received in HELLO packet. |
function M:_onNewClient( msg, ip, port )
Callback called when new client connection is detected. It opens the new private connected UDP socket for the client, or calls onMaxClient if the limit is exceeded. Finally it calls <onNewClient>().
msg | (string) message |
ip | (string) IP address |
port | (number) port |
function M:onBadToken( msg, ip, port )
Called when bad token is received in HELLO packet. The server expects a “HELLO <token>” as a first message from new client. See setToken.
msg | (string) original packet payload |
ip | (string) client ip |
port | (number) client port |
Sets the timeout for all current and newly opened sockets.
function M:setTimeout( t )
Sets the debugging.
function M:setDebug( d )
Sets the token for handshaking.
function M:setToken( t )
Starts the server.
function M:start( port, host, timeout )
Sends a message to all connected sockets (clients).
function M:sendAll( msg )
Sends a message to a connected socket.
function M:send( msg, sock )
Returns the number of packets sent out.
function M:getSentPackets()
Returns the number of packets received.
function M:getReceivedPackets()
Sends a ping message to server (in client mode) or all connected sockets (in server mode).
function M:ping( sock )
Keeps the client/server running.
function M:step( dt )
Close the connection.
function M:close( skt )
Connects as a client to the server.
function M:connect( host, port, timeout )
Called when no packet is received in some amount of time.
function M:_onTimeout()
Callback called when new client connection is detected.
function M:_onNewClient( msg, ip, port )
Sets the peer for the socket, makes socket connected.
function M:_setPeer( sock, rip, rport )
Unsets the peer for the socket, makes socket unconnected.
function M:_unsetPeer( sock )
Called when new packed arrives on connected socket.
function M:_onNewMessage( msg, sock, ip, port )
Called when new client is connected.
function M:onNewClient( msg, ip, port, ipc, portc )
Called when new message arrives.
function M:onNewMessage( msg, sock )
Called when a socket is disconnected.
function M:onDisconnect( sock )
Called when client connected to the Server, or a new client has connected.
function M:onConnect( sock, ip, port )
Called when client or server closed the connection.
function M:onClose( sock )
Called when client is connecting to the Server on a final port.
function M:onConnecting( sock, ip, port )
Called when the connection lags.
function M:onStalled( sock )
Connection is no longer stalled.
function M:onUnstalled( sock )
Called when too much client connections is open.
function M:onMaxClients( msg, ip, port )
Called when PONG packet is received.
function M:onPong( sock )
Called when PING packet is received.
function M:onPing( sock )
Called when bad token is received in HELLO packet.
function M:onBadToken( msg, ip, port )