Core Function SSListen
From Sputnik Wiki
SSListen( <port>, <max connections>, <name> )
Contents |
Description
Create a server and begin listening for clients to connect.
Parameters
port
The port to use for the server.
max connections
Optional; The max amount of users to allow onto the server.
Default is 32.
name
Optional; The name of the server/client program (Client and server should use same name)
Return Value
Success: Returns a new server object.
Failure: Returns 0.
Remarks
This is NOT a generic socket toolset; You can't use this to connect to anything instead it is used to connect Sputnik clients with Sputnik servers in a very easy to use way.
Example
See the Connect() example for the client to go with this server example
Complete server example:
my $Clients = array(); // Create an array to hold all clients that connect $Port = 5500; // The port to listen on $Serv = SSListen($Port); // Server a server on port 5500 println("Begin listening on port $Port"); println("Waiting for connections....."); While( True ) // Keep the server running forever { While( $Data = SSRecv($Serv) ) // Check if we got any messages from any clients { $MSG = $Data[0]; // The message ID $CLIENT = $Data[1]; // The client object $ClientID = $Data[2]; // The clients unique ID $ClientIP = $Data[3]; // The clients IP Switch ($MSG) // Check what type of message we just got { case @ssData: // The message is data usually text and chat etc { $dCode = SSRead($Serv, "i"); // Read an Int32 "i" from the recieved message switch ($dCode) // Check what type of code we got { case 1: // The code is a Chat message { $Text = SSRead($Serv, "p"); // Read a string "p" from the recieved message $Text = Trim($Text); // Remove any trailing @CR or @LF etc $Name = $Clients[$ClientID][0]; // Get client name // Say what client sent it to ocnsole println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' Said '$Text'"); // Check is client is being abusive if($Text =~ m/fuck/i) // Kick client for swearing { // Kick the client from server SSDrop($Serv, $CLIENT, "Profanity is not allowed here!"); unset($Clients[$ClientID]); // Remove the client from client list } else { $senderClient = $Clients[$ClientID]; // Find current client the one who sent the message // Create a message send buffer $sBuf = SSSendBufferCreate($Serv); // Create the buffer SSSendBufferAppend($sBuf, "i", 1); // Add an Int32 with 1 for the ID $Time = "(" . @Hour . ":" . @Min . ":" . @Sec . ") "; // Time message recieved SSSendBufferAppend($sBuf, "p", $Time . $senderClient[0] . ": $Text"); // Add a string // Loop through all clients foreach($Clients as $cID => $cli) // Key Client ID, Value Client object { // Send the buffer we made above to this client if its still connected if(SSClientStatus($Serv, $cli[1]) == @ssConnected) SSSend($Serv, $sBuf, $cli[1]); } } } break; } } break; case @ssConnectionApproval: // A client is asking for permission to join our server { $Name = SSRead($Serv, "p"); // Read the clients name from the connection string // Show message on console println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' is asking permission to join the server"); //SSDisapprove($CLIENT, "We dont want you here"); // Use this to reject the client SSApprove($CLIENT); // Approve the client $Clients[$ClientID] = array($Name, $CLIENT); // Add the client to the client array list } break; case @ssStatusChanged: // A client has changed its status { $ClientStatus = SSClientStatus($Serv, $CLIENT); // Find new status Switch ($ClientStatus) // Check what type of status change { case @ssConnected: // The client reports its now fully connected to our server { $Name = $Clients[$ClientID][0]; // Get client name println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' has joined the server"); } break; case @ssDisconnected: // The client reports its disconnected from the server { $Name = $Clients[$ClientID][0]; // Get client name println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' has left the server"); unset($Clients[$ClientID]); // Remove the client from the client array list } break; case @ssConnecting: // A client is connecting case @ssReconnecting: // A client is reconnected case @ssDisconnecting: // A client is disconnecting break; } } break; } } }