Websocket plugin for squirrel

Started by habi2, Sep 16, 2024, 09:14 AM

Previous topic - Next topic

habi2

Hi this is a project i have been building for past two weeks.

Websocket is a communication protocol over tcp, which enables bi-directional communication between server and client. Before, the client connects to server and wait for server response. While it is waiting it cannot do anything,  unless 'select' is used. However, here server can send anytime it wants (eg.when a player joined) and client have callback when it receive messages from server.

Here is an example:
function onPlayerJoin(player)
{
Broadcast(player.Name + " has joined the server");
}
function onopen(client)
{

print( format( "client IP:%s:%d joined." , client.ip, client.port ));

print( "Sending client message 'Hello'" );

client.SendMessage( "Hello" );

}

//Called when a client disconnects
function onclose(client)
{
print("Connection closed " + client.ip + ":" + client.port );
}



In the above picture, a client is connecting to server  and is sending messages.
//Called when a client send text messages
function onmessage(client, message)
{
print( format( "client %s:%d :%s" , client.ip, client.port, message ) );
}
You can print the message send by the client  to console or message in game.
Not only text messages, binary messages are also supported using  blobs.

To start the websocket server, you have to call one-time function WebsocketInit with parameters like
        local host="127.0.0.1"; // Server IP address
local port=8080 ; // WebSocket server port
local timeout_ms=10000 ; // Timeout in milliseconds

// Initialize WebSocket server with the provided callbacks
WebsocketInit(host,port,timeout_ms,onopen,onclose,onmessage, onbinmessage);

Another interesting feature is the properties of client:
//keys of table client
ip  - string
port  - integer
SendMessage([string] message): Sends a text message to the client.
SendBinMessage([blob] data): Sends a binary message to the client using a blob.
close(): Closes the client's connection.

Download Links, Source, Credits and License Information
The project is licensed under GPL v3.
Credits: Davidson Francis for WebSocket server library.

Download the plugin along with source, instructions, build-instructions and examples below
Link 1

Alternative Link