Other-wise known as "SWSocket" connections.
Most servers would use MySQL or a database for this. Using cron jobs.
What is this?
This is basically a way to connect to a HTTP socket type.
Why would we want to do this?
You can make many things from your website if you have this. You can execute commands via the socket, you can send items etc whatnot.
How do i use?
You first have to make a web function.
Code:
function SendMUSData($data){
$mus_ip = localhost;
$mus_port = 30;
if(!is_numeric($mus_port)){ echo "<b>System Error</b><br />Invalid MUS Port!"; exit; }
$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($sock, $mus_ip, $mus_port);
if(!is_resource($sock)){
return false;
} else {
socket_send($sock, $data, strlen($data), MSG_DONTROUTE);
return true;
}
socket_close($sock);
}
usage: SendMUSData(CODE_HERE);
Then you will need to add a MUS socket server to your runescape private emulator/server.
Code:
/************************************************\
* ############################################ *
* ## RuneScape C# Emuator ## *
* ############################################ *
* ## Copyright (c) 2006 - 2009 AJ R ## *
* ## http://theajp.com/ <[email protected]> ## *
* ############################################ *
\************************************************/
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Collections;
using RuneScape;
namespace RuneScape.Net
{
/// <summary>
/// Asynchronous socket server for the MUS connections.
/// </summary>
public class musSocket
{
private static Socket socketHandler;
private static int _Port;
private static string _musHost;
/// <summary>
/// Initializes the socket listener for MUS connections and starts listening.
/// </summary>
/// <param name="bindPort">The port where the socket listener should be bound to.</param>
/// <remarks></remarks>
internal static bool Init(int bindPort, string musHost)
{
_Port = bindPort;
_musHost = musHost;
socketHandler = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Logger.WriteLine("Starting up asynchronous socket server for MUS connections for port " + bindPort + "...");
try
{
socketHandler.Bind(new IPEndPoint(IPAddress.Any, bindPort));
socketHandler.Listen(25);
socketHandler.BeginAccept(new AsyncCallback(connectionRequest), socketHandler);
Logger.WriteLine("Asynchronous socket server for MUS connections running on port " + bindPort);
Logger.WriteLine("Listening for MUS connections from " + musHost);
return true;
}
catch
{
Logger.WriteError("Error while setting up asynchronous socket server for MUS connections on port " + bindPort);
Logger.WriteError("Port " + bindPort + " could be invalid or in use already.");
return false;
}
}
private static void connectionRequest(IAsyncResult iAr)
{
Socket newSocket = ((Socket)iAr.AsyncState).EndAccept(iAr);
if (newSocket.RemoteEndPoint.ToString().Split(':')[0] != _musHost)
{
newSocket.Close();
return;
}
musConnection newConnection = new musConnection(newSocket);
socketHandler.BeginAccept(new AsyncCallback(connectionRequest), socketHandler);
}
/// <summary>
/// Represents an asynchronous, one time usage, socket connection between the emulator and HoloCMS, used for various live updates between site & server.
/// </summary>
private class musConnection
{
private Socket Connector;
private byte[] dataBuffer = new byte[10001];
/// <summary>
/// Initializes the musConnection and listens for one single packet, processes it and closes the connection. On any error, the connection is closed.
/// </summary>
/// <param name="Connector">The socket of the musConnection.</param>
internal musConnection(Socket Connector)
{
this.Connector = Connector;
Connector.BeginReceive(dataBuffer, 0, dataBuffer.Length, SocketFlags.None, new AsyncCallback(dataArrival), null);
}
/// <summary>
/// Called when a packet is received, the packet will be processed and the connection will be closed (after processing packet) in all cases. No errors will be thrown.
/// </summary>
/// <param name="iAr"></param>
private void dataArrival(IAsyncResult iAr)
{
try
{
int bytesReceived = Connector.EndReceive(iAr);
string Data = System.Text.Encoding.ASCII.GetString(dataBuffer, 0, bytesReceived);
string musHeader = Data.Substring(0, 4);
string[] musData = Data.Substring(4).Split(Convert.ToChar(2));
Logger.WriteLine("DATA: " + Data);
#region MUS trigger commands
// Unsafe code, but on any error it jumps to the catch block & disconnects the socket
switch (musHeader)
{
case "ITEM":
{
int mUserId = int.Parse(musData[0]);
int mItemId = int.Parse(musData[1]);
int mItemAmt = int.Parse(musData[2]);
JoltEnvironment.GetRuneScape().GetEngine().GetPlayerSession().GetItems().AddItem(mUserId, mItemId, mItemAmt)
break;
}
}
#endregion
}
catch { }
finally { killConnection(); }
}
/// <summary>
/// Closes the connection and destroys the object.
/// </summary>
private void killConnection()
{
try { Connector.Close(); }
catch { }
}
}
}
}
Usage to execute the case:
SendMUSData('ITEM' . USERID . ITEMID . ITEMAMT);
Template:
Code:
case "ITEM":
{
int mUserId = int.Parse(musData[0]);
int mItemId = int.Parse(musData[1]);
int mItemAmt = int.Parse(musData[2]);
JoltEnvironment.GetRuneScape().GetEngine().GetPlayerSession().GetItems().AddItem(mUserId, mItemId, mItemAmt)
break;
}