Core Function SSConnect

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Example)
(Example)
 
(4 intermediate revisions by one user not shown)
Line 42: Line 42:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 +
// Load the old GUI system
 +
// Since this an old GUI example
 +
use('SputnikOldGUI.dll', true);
 +
 
// 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 );
 
   
 
   
 
// Create the GUI objects
 
// Create the GUI objects
$obj_textbox = GUICreate("TextBoxEx", $GUI, "", 5, 15, 610,  370);
+
Global $obj_textbox = GUICreate("TextBoxEx", $GUI, "", 5, 15, 610,  370);
$obj_RealSend = GUICreate("Button", $GUI, "Send", 505, 410);
+
Global $obj_RealSend = GUICreate("Button", $GUI, "Send", 505, 410);
$obj_CMD = GUICreate("TextBoxEx", $GUI, "", 10, 405, 485,  25);
+
Global $obj_CMD = GUICreate("TextBoxEx", $GUI, "", 10, 405, 485,  25);
 
   
 
   
 
// Add a few links
 
// Add a few links
 
GUILink($obj_RealSend, "Click", 'Send();');
 
GUILink($obj_RealSend, "Click", 'Send();');
GUILink($obj_CMD, "KeyPress", 'KeyPress();');
+
GUILink($obj_CMD, "KeyPress", 'KeyPress($arg);');
 
   
 
   
 
// Allow the user to enter a name to use on the server
 
// 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");
+
my $Name = InputBox("Sputnik Client", "Enter a name to use on the chat", "MyName");
 
   
 
   
 
// Join Server
 
// Join Server
Glob $Client = SSConnect("81.102.115.125", 5500, $Name);
+
Global $Client = SSConnect("127.0.0.1", 5570, $Name);
 
   
 
   
 
// Loop while GUI is active
 
// Loop while GUI is active
Line 65: Line 69:
 
{  
 
{  
 
DoEvents(); // DoEvents to keep the GUI working fine
 
DoEvents(); // DoEvents to keep the GUI working fine
While( $Data = SSRecv($Client) ) // Check if we recieved a message from server
+
While( my $Data = SSRecv($Client) ) // Check if we recieved a message from server
 
{
 
{
$MSG = $Data[0]; // Get the message ID
+
my $MSG = $Data[0]; // Get the message ID
 
Switch ($MSG) // CHeck what type of message it was
 
Switch ($MSG) // CHeck what type of message it was
 
{
 
{
 
case @ssStatusChanged: // The message says we changed status
 
case @ssStatusChanged: // The message says we changed status
 
{
 
{
$Status = SSClientStatus($Client); // Get our status
+
my $Status = SSClientStatus($Client); // Get our status
 
Switch ($Status) // Check our status
 
Switch ($Status) // Check our status
 
{
 
{
 
case @ssDisconnected: // We have been disconnected from the server
 
case @ssDisconnected: // We have been disconnected from the server
 
{
 
{
$fread = SSRead($Client);
+
my $fread = SSRead($Client, "p");
 
AddText("Disconnected from server reason: $fread");
 
AddText("Disconnected from server reason: $fread");
 
}
 
}
Line 100: Line 104:
 
case @ssConnectionRejected: // Our connection has been rejected on the server
 
case @ssConnectionRejected: // Our connection has been rejected on the server
 
{
 
{
$fread = SSRead($Client); // Read the reason why we was rejected
+
my $fread = SSRead($Client, "p"); // Read the reason why we was rejected
 
AddText("Server rejected our connection reason: $fread"); // Add it to display
 
AddText("Server rejected our connection reason: $fread"); // Add it to display
 
}
 
}
Line 106: Line 110:
 
case @ssData: // We have recieved data from server
 
case @ssData: // We have recieved data from server
 
{
 
{
$dCode = SSRead($Client, "i"); // Read the Int32 "i" code of the message
+
my $dCode = SSRead($Client, "i"); // Read the Int32 "i" code of the message
 +
say "got msg id $dCode";
 
switch ($dCode)
 
switch ($dCode)
 
{
 
{
 
case 1: // It is a Chat message
 
case 1: // It is a Chat message
 
{
 
{
$Text = SSRead($Client, "p"); // Read the string "p" from the message
+
my $Text = SSRead($Client, "p"); // Read the string "p" from the message
 +
say "got msg txt $Text";
 
AddText($Text); // Add it to the display
 
AddText($Text); // Add it to the display
 
}
 
}
Line 123: Line 129:
 
   
 
   
 
// When a key is presed on chat send box
 
// When a key is presed on chat send box
Function KeyPress()
+
Function KeyPress($arg)
 
{
 
{
 
if($arg == @LF || $arg == @CR) // Make sure its a valid enter key
 
if($arg == @LF || $arg == @CR) // Make sure its a valid enter key
Line 132: Line 138:
 
Function AddText($str)
 
Function AddText($str)
 
{
 
{
$Text = GUIGetProp($obj_textbox, "Text"); // Read the current "Text"
+
my $Text = GUIGetProp($obj_textbox, "Text"); // Read the current "Text"
 
$Text = $str . @CRLF . $Text; // Add new text to top
 
$Text = $str . @CRLF . $Text; // Add new text to top
 
GUISetProp($obj_textbox, "Text", $Text); // Set the "Text"
 
GUISetProp($obj_textbox, "Text", $Text); // Set the "Text"
Line 140: Line 146:
 
Function Send()
 
Function Send()
 
{
 
{
$Text = GUIGetProp($obj_CMD, "Text"); // Read current text to send
+
my $Text = GUIGetProp($obj_CMD, "Text"); // Read current text to send
 
GUISetProp($obj_CMD, "Text", ""); // Clear the send box
 
GUISetProp($obj_CMD, "Text", ""); // Clear the send box
$sBuf = SSSendBufferCreate($Client); // Create a new buffer to send a message with
+
my $sBuf = SSBufferNew($Client); // Create a new buffer to send a message with
SSSendBufferAppend($sBuf, "i", 1); // Add the code using an Int32 "i"
+
SSBufferPut($sBuf, "i", 1); // Add the code using an Int32
SSSendBufferAppend($sBuf, "p", $Text); // Add the string "p" to send
+
SSBufferPut($sBuf, "p", $Text); // Add the string to send
 
SSSend($Client, $sBuf); // Send the message to the server
 
SSSend($Client, $sBuf); // Send the message to the server
 +
unset($sBuf); // Free up resources/ram by wiping the buffer
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 06:13, 15 June 2015

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:

// Load the old GUI system
// Since this an old GUI example
use('SputnikOldGUI.dll', true);
 
// Create a new GUI
Global $GUI = GUICreate("Window", "GUI", 640, 480);
GUILoad( $GUI );
 
// Create the GUI objects
Global $obj_textbox = GUICreate("TextBoxEx", $GUI, "", 5, 15, 610,  370);
Global $obj_RealSend = GUICreate("Button", $GUI, "Send", 505, 410);
Global $obj_CMD = GUICreate("TextBoxEx", $GUI, "", 10, 405, 485,  25);
 
// Add a few links
GUILink($obj_RealSend, "Click", 'Send();');
GUILink($obj_CMD, "KeyPress", 'KeyPress($arg);');
 
// Allow the user to enter a name to use on the server
my $Name = InputBox("Sputnik Client", "Enter a name to use on the chat", "MyName");
 
// Join Server
Global $Client = SSConnect("127.0.0.1", 5570, $Name);
 
// Loop while GUI is active
While ( GUIStatus( $GUI ) )
{ 
	DoEvents(); // DoEvents to keep the GUI working fine
	While( my $Data = SSRecv($Client) ) // Check if we recieved a message from server
	{
		my $MSG = $Data[0]; // Get the message ID
		Switch ($MSG) // CHeck what type of message it was
		{
			case @ssStatusChanged: // The message says we changed status
			{
				my $Status = SSClientStatus($Client); // Get our status
				Switch ($Status) // Check our status
				{
					case @ssDisconnected: // We have been disconnected from the server
					{
						my $fread = SSRead($Client, "p");
						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
			{
				my $fread = SSRead($Client, "p"); // 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
			{
				my $dCode = SSRead($Client, "i"); // Read the Int32 "i" code of the message
				say "got msg id $dCode";
				switch ($dCode)
				{
					case 1: // It is a Chat message
					{
						my $Text = SSRead($Client, "p"); // Read the string "p" from the message
						say "got msg txt $Text";
						AddText($Text); // Add it to the display
					}
					break;
				}
			}
			break;
		}
	}
}
 
// When a key is presed on chat send box
Function KeyPress($arg)
{
	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)
{
	my $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()
{
	my $Text = GUIGetProp($obj_CMD, "Text"); // Read current text to send
	GUISetProp($obj_CMD, "Text", ""); // Clear the send box
	my $sBuf = SSBufferNew($Client); // Create a new buffer to send a message with
	SSBufferPut($sBuf, "i", 1); // Add the code using an Int32
	SSBufferPut($sBuf, "p", $Text); // Add the string to send
	SSSend($Client, $sBuf); // Send the message to the server
	unset($sBuf); // Free up resources/ram by wiping the buffer
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox