Core Function SSConnect

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(Example)
Line 43: Line 43:
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 
// Create a new GUI
 
// Create a new GUI
Glob $GUI = GUICreate("Window", "GUI", 640, 480);
+
Global $GUI = GUICreate("Window", "GUI", 640, 480);
 
GUILoad( $GUI );
 
GUILoad( $GUI );
 
   
 
   
Line 59: Line 59:
 
   
 
   
 
// Join Server
 
// Join Server
Glob $Client = SSConnect("81.102.115.125", 5500, $Name);
+
Global $Client = SSConnect("81.102.115.125", 5500, $Name);
 
   
 
   
 
// Loop while GUI is active
 
// Loop while GUI is active

Revision as of 00:11, 18 January 2013

SSConnect( <ip>, <port>, <connection string>, <name>  )

Contents

Description

Connect to a server.

Parameters

ip

The IP of the server.

port

The port of the server.

connection string

A string to send the server upon connecting (This is usually the name of the client etc).

name

Optional; The name of the server/client program (Client and server should use same name)

Return Value

Success: Returns a new client 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 Listen() example for the server to go with this client example

Complete client example:

// Create a new GUI
Global $GUI = GUICreate("Window", "GUI", 640, 480);
GUILoad( $GUI );
 
// Create the GUI objects
$obj_textbox = GUICreate("TextBoxEx", $GUI, "", 5, 15, 610,  370);
$obj_RealSend = GUICreate("Button", $GUI, "Send", 505, 410);
$obj_CMD = GUICreate("TextBoxEx", $GUI, "", 10, 405, 485,  25);
 
// Add a few links
GUILink($obj_RealSend, "Click", 'Send();');
GUILink($obj_CMD, "KeyPress", 'KeyPress();');
 
// Allow the user to enter a name to use on the server
$Name = InputBox("Sputnik Client", "Enter a name to use on the chat", "MyName");
 
// Join Server
Global $Client = SSConnect("81.102.115.125", 5500, $Name);
 
// Loop while GUI is active
While ( GUIStatus( $GUI ) )
{ 
	DoEvents(); // DoEvents to keep the GUI working fine
	While( $Data = SSRecv($Client) ) // Check if we recieved a message from server
	{
		$MSG = $Data[0]; // Get the message ID
		Switch ($MSG) // CHeck what type of message it was
		{
			case @ssStatusChanged: // The message says we changed status
			{
				$Status = SSClientStatus($Client); // Get our status
				Switch ($Status) // Check our status
				{
					case @ssDisconnected: // We have been disconnected from the server
					{
						$fread = SSRead($Client);
						AddText("Disconnected from server reason: $fread");
					}
					break;
					case @ssConnected: // We have connected to the server
					{
						AddText("We have connected to the server");
					}
					break;
					case @ssConnecting: // We are connecting to the server
					case @ssReconnecting: // We are reconnecting to the server
					case @ssDisconnecting: // We are disconnecting from the server
					break;
				}
			}
			break;
			case @ssConnectionApproval: // Our connection has been approved on the server
			{
				AddText("Connected to server!");
			}
			break;
			case @ssConnectionRejected: // Our connection has been rejected on the server
			{
				$fread = SSRead($Client); // Read the reason why we was rejected
				AddText("Server rejected our connection reason: $fread"); // Add it to display
			}
			break;
			case @ssData: // We have recieved data from server
			{
				$dCode = SSRead($Client, "i"); // Read the Int32 "i" code of the message
				switch ($dCode)
				{
					case 1: // It is a Chat message
					{
						$Text = SSRead($Client, "p"); // Read the string "p" from the message
						AddText($Text); // Add it to the display
					}
					break;
				}
			}
			break;
		}
	}
}
 
// When a key is presed on chat send box
Function KeyPress()
{
	if($arg == @LF || $arg == @CR) // Make sure its a valid enter key
		Send(); // Send the message
}
 
// Function to add text to output display
Function AddText($str)
{
	$Text = GUIGetProp($obj_textbox, "Text"); // Read the current "Text"
	$Text = $str . @CRLF . $Text; // Add new text to top
	GUISetProp($obj_textbox, "Text", $Text); // Set the "Text"
}
 
// Send a message to server
Function Send()
{
	$Text = GUIGetProp($obj_CMD, "Text"); // Read current text to send
	GUISetProp($obj_CMD, "Text", ""); // Clear the send box
	$sBuf = SSSendBufferCreate($Client); // Create a new buffer to send a message with
	SSSendBufferAppend($sBuf, "i", 1); // Add the code using an Int32 "i"
	SSSendBufferAppend($sBuf, "p", $Text); // Add the string "p" to send
	SSSend($Client, $sBuf); // Send the message to the server
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox