Core Function SSListen
From Sputnik Wiki
(Difference between revisions)
(→Example) |
(→Example) |
||
(4 intermediate revisions by one user not shown) | |||
Line 27: | Line 27: | ||
Success: Returns a new server object. | Success: Returns a new server object. | ||
− | Failure: Returns | + | Failure: Returns null. |
=== Remarks === | === Remarks === | ||
Line 41: | Line 41: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
my $Clients = array(); // Create an array to hold all clients that connect | my $Clients = array(); // Create an array to hold all clients that connect | ||
− | $Port = | + | my $Port = 5570; // The port to listen on |
− | $Serv = SSListen($Port); // Server a server on port 5500 | + | my $Serv = SSListen($Port); // Server a server on port 5500 |
println("Begin listening on port $Port"); | println("Begin listening on port $Port"); | ||
println("Waiting for connections....."); | println("Waiting for connections....."); | ||
While( True ) // Keep the server running forever | While( True ) // Keep the server running forever | ||
{ | { | ||
− | While( $Data = SSRecv($Serv) ) // Check if we got any messages from any clients | + | While( my $Data = SSRecv($Serv) ) // Check if we got any messages from any clients |
{ | { | ||
− | $MSG = $Data[0]; // The message ID | + | my $MSG = $Data[0]; // The message ID |
− | $CLIENT = $Data[1]; // The client object | + | my $CLIENT = $Data[1]; // The client object |
− | $ClientID = $Data[2]; // The clients unique ID | + | my $ClientID = $Data[2]; // The clients unique ID |
− | $ClientIP = $ | + | my $ClientIP = SSClientIP($CLIENT); // The clients IP |
Switch ($MSG) // Check what type of message we just got | Switch ($MSG) // Check what type of message we just got | ||
{ | { | ||
case @ssData: // The message is data usually text and chat etc | case @ssData: // The message is data usually text and chat etc | ||
{ | { | ||
− | $dCode = SSRead($Serv, "i"); // Read an Int32 "i" from the recieved message | + | my $dCode = SSRead($Serv, "i"); // Read an Int32 "i" from the recieved message |
+ | say "got msg id $dCode"; | ||
switch ($dCode) // Check what type of code we got | switch ($dCode) // Check what type of code we got | ||
{ | { | ||
case 1: // The code is a Chat message | case 1: // The code is a Chat message | ||
{ | { | ||
− | $Text = SSRead($Serv, "p"); // Read a string "p" from the recieved message | + | my $Text = SSRead($Serv, "p"); // Read a string "p" from the recieved message |
$Text = Trim($Text); // Remove any trailing @CR or @LF etc | $Text = Trim($Text); // Remove any trailing @CR or @LF etc | ||
− | $Name = $Clients[$ClientID][0]; // Get client name | + | my $Name = $Clients[$ClientID][0]; // Get client name |
// Say what client sent it to ocnsole | // Say what client sent it to ocnsole | ||
println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' Said '$Text'"); | println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' Said '$Text'"); | ||
Line 71: | Line 72: | ||
{ | { | ||
// Kick the client from server | // Kick the client from server | ||
− | SSDrop( | + | SSDrop($CLIENT, "Profanity is not allowed here!"); |
unset($Clients[$ClientID]); // Remove the client from client list | unset($Clients[$ClientID]); // Remove the client from client list | ||
} | } | ||
else | else | ||
{ | { | ||
− | $senderClient = $Clients[$ClientID]; // Find current client the one who sent the message | + | my $senderClient = $Clients[$ClientID]; // Find current client the one who sent the message |
// Create a message send buffer | // Create a message send buffer | ||
− | $sBuf = | + | my $sBuf = SSBufferNew($Serv); // Create the buffer |
− | + | SSBufferPut($sBuf, "i", 1); // Add an Int32 with 1 for the ID (cast to make sure of int) | |
− | $Time = "(" . @Hour . ":" . @Min . ":" . @Sec . ") "; // Time message recieved | + | my $Time = "(" . @Hour . ":" . @Min . ":" . @Sec . ") "; // Time message recieved |
− | + | SSBufferPut($sBuf, "p", $Time . $senderClient[0] . ": $Text"); // Add a string | |
// Loop through all clients | // Loop through all clients | ||
− | foreach($Clients as $cID => $cli) // Key Client ID, Value Client object | + | foreach($Clients as my $cID => my $cli) // Key Client ID, Value Client object |
{ | { | ||
// Send the buffer we made above to this client if its still connected | // Send the buffer we made above to this client if its still connected | ||
− | if(SSClientStatus( | + | if(SSClientStatus($cli[1]) == @ssConnected) |
− | SSSend($Serv | + | SSSend($Serv, $cli[1], $sBuf); |
} | } | ||
+ | unset($sBuf); // Free up resources/ram by wiping the buffer | ||
} | } | ||
} | } | ||
Line 97: | Line 99: | ||
case @ssConnectionApproval: // A client is asking for permission to join our server | case @ssConnectionApproval: // A client is asking for permission to join our server | ||
{ | { | ||
− | $Name = SSRead($Serv, "p"); // Read the clients name from the connection string | + | my $Name = SSRead($Serv, "p"); // Read the clients name from the connection string |
// Show message on console | // Show message on console | ||
println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' is asking permission to join the server"); | println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' is asking permission to join the server"); | ||
Line 107: | Line 109: | ||
case @ssStatusChanged: // A client has changed its status | case @ssStatusChanged: // A client has changed its status | ||
{ | { | ||
− | $ClientStatus = SSClientStatus( | + | my $ClientStatus = SSClientStatus($CLIENT); // Find new status |
Switch ($ClientStatus) // Check what type of status change | Switch ($ClientStatus) // Check what type of status change | ||
{ | { | ||
case @ssConnected: // The client reports its now fully connected to our server | case @ssConnected: // The client reports its now fully connected to our server | ||
{ | { | ||
− | $Name = $Clients[$ClientID][0]; // Get client name | + | my $Name = $Clients[$ClientID][0]; // Get client name |
println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' has joined the server"); | println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' has joined the server"); | ||
} | } | ||
Line 118: | Line 120: | ||
case @ssDisconnected: // The client reports its disconnected from the server | case @ssDisconnected: // The client reports its disconnected from the server | ||
{ | { | ||
− | $Name = $Clients[$ClientID][0]; // Get client name | + | my $Name = $Clients[$ClientID][0]; // Get client name |
println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' has left the server"); | println("Client ID '$ClientID' IP '$ClientIP' Name '$Name' has left the server"); | ||
unset($Clients[$ClientID]); // Remove the client from the client array list | unset($Clients[$ClientID]); // Remove the client from the client array list |
Latest revision as of 06:13, 15 June 2015
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 null.
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 my $Port = 5570; // The port to listen on my $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( my $Data = SSRecv($Serv) ) // Check if we got any messages from any clients { my $MSG = $Data[0]; // The message ID my $CLIENT = $Data[1]; // The client object my $ClientID = $Data[2]; // The clients unique ID my $ClientIP = SSClientIP($CLIENT); // The clients IP Switch ($MSG) // Check what type of message we just got { case @ssData: // The message is data usually text and chat etc { my $dCode = SSRead($Serv, "i"); // Read an Int32 "i" from the recieved message say "got msg id $dCode"; switch ($dCode) // Check what type of code we got { case 1: // The code is a Chat message { my $Text = SSRead($Serv, "p"); // Read a string "p" from the recieved message $Text = Trim($Text); // Remove any trailing @CR or @LF etc my $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($CLIENT, "Profanity is not allowed here!"); unset($Clients[$ClientID]); // Remove the client from client list } else { my $senderClient = $Clients[$ClientID]; // Find current client the one who sent the message // Create a message send buffer my $sBuf = SSBufferNew($Serv); // Create the buffer SSBufferPut($sBuf, "i", 1); // Add an Int32 with 1 for the ID (cast to make sure of int) my $Time = "(" . @Hour . ":" . @Min . ":" . @Sec . ") "; // Time message recieved SSBufferPut($sBuf, "p", $Time . $senderClient[0] . ": $Text"); // Add a string // Loop through all clients foreach($Clients as my $cID => my $cli) // Key Client ID, Value Client object { // Send the buffer we made above to this client if its still connected if(SSClientStatus($cli[1]) == @ssConnected) SSSend($Serv, $cli[1], $sBuf); } unset($sBuf); // Free up resources/ram by wiping the buffer } } break; } } break; case @ssConnectionApproval: // A client is asking for permission to join our server { my $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 { my $ClientStatus = SSClientStatus($CLIENT); // Find new status Switch ($ClientStatus) // Check what type of status change { case @ssConnected: // The client reports its now fully connected to our server { my $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 { my $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; } } }