Skip to content
This repository has been archived by the owner on Aug 1, 2019. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Kolkmeier committed Nov 15, 2010
0 parents commit b0eeebc
Show file tree
Hide file tree
Showing 7 changed files with 594 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

// Inherit from SocketIoClient Class
public class Example : SocketIoClient {

public GameObject cube;

void Awake() {
// Setup Socket Connection
SetupClient("ws://ewi1544.ewi.utwente.nl:5000/socket.io/websocket");
}


void Start() {
StartClient();
}

public void Update() {
// Calls "HandleMessage" if a message was on stack
ProcessQueue();
cube.transform.RotateAround(Vector3.up, Time.deltaTime/5.0f);
}

public override void HandleMessage(string msg) {
switch(msg) {
case "red": cube.transform.renderer.material.color = Color.red; break;
case "green": cube.transform.renderer.material.color = Color.green; break;
case "blue": cube.transform.renderer.material.color = Color.blue; break;
default: print("Unknown: " + msg); break;
}
}

}
Empty file added README
Empty file.
53 changes: 53 additions & 0 deletions lib/SocketIoClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using UnityEngine;
using System;
using System.Collections.Generic;


public class SocketIoClient : MonoBehaviour {

[HideInInspector]
public Queue<string> queue;

public SocketIoClientConnection websocket;

public void ProcessQueue() {
while(queue.Count>0) {
HandleMessage(queue.Dequeue());
}
}

public virtual void HandleMessage(string msg) {
print("SocketIoClient: " + msg);
}

public virtual void OnOpen() {
print("SocketIoClient: [open]");
}

public virtual void OnClose() {
print("SocketIoClient: [closed]");
}

public virtual void Log(string msg) {
print(msg);
}

public virtual void OnApplicationQuit() {
websocket.close();
}

public void SetupClient(string url) {
try {
websocket = new SocketIoClientConnection(new Uri(url));
websocket.setEventHandler(this);
websocket.connect();
} catch (SocketIoClientException wse) {
print(wse.ToString());
}

}

public void StartClient() {
queue = new Queue<string>();
}
}
216 changes: 216 additions & 0 deletions lib/SocketIoClientConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
using System.Collections.Generic;


public class SocketIoClientConnection {
private Uri url = null;

private SocketIoClient eventHandler = null;

private volatile bool connected = false;


public TcpClient socket = null;
public Stream stream = null;
private StreamReader input = null;

private SocketIoClientReceiver receiver = null;
private SocketIoClientHandshake handshake = null;

private System.Object lockThis = new System.Object();

public SocketIoClientConnection(Uri url) : this(url, null) {}

public SocketIoClientConnection(Uri url, string protocol) {
this.url = url;
handshake = new SocketIoClientHandshake(url, protocol);
}


public void setEventHandler(SocketIoClient eventHandler) {
this.eventHandler = eventHandler;
}


public SocketIoClient getEventHandler() {
return this.eventHandler;
}


public void connect() {
try {
if (connected) {
throw new SocketIoClientException("already connected");
}

socket = createSocket();
stream = socket.GetStream();

input = new StreamReader(stream);
byte[] bytes = handshake.getHandshake();
stream.Write(bytes, 0, bytes.Length);
stream.Flush();

bool handshakeComplete = false;
List<string> handshakeLines = new List<string>();
string line;

while(!handshakeComplete) {
line = input.ReadLine().Trim();
if (line.Length>0) {
handshakeLines.Add(line);
} else {
handshakeComplete = true;
}
}

char[] response = new char[16];
input.ReadBlock(response, 0, response.Length);

handshake.verifyServerStatusLine(handshakeLines[0]);

/* Verifying handshake fails... */
//handshake.verifyServerResponse(response);

handshakeLines.RemoveAt(0);

Dictionary<string, string> headers = new Dictionary<string, string>();
foreach (string l in handshakeLines) {
string[] keyValue = l.Split(new char[] {':'},2);
headers.Add(keyValue[0].Trim(), keyValue[1].Trim());
}

handshake.verifyServerHandshakeHeaders(headers);
receiver = new SocketIoClientReceiver(this);
connected = true;
eventHandler.OnOpen();
(new Thread(receiver.run)).Start();
} catch (SocketIoClientException wse) {
throw wse;
} catch (IOException ioe) {
throw new SocketIoClientException("error while connecting: " + ioe.StackTrace, ioe);
}
}

public void send(string data) {
lock (lockThis) {
if (!connected) {
throw new SocketIoClientException("error while sending text data: not connected");
}

try {
byte[] msg = Encoding.UTF8.GetBytes(data);
byte[] length = Encoding.UTF8.GetBytes(msg.Length+"");
stream.WriteByte(0x00);

stream.WriteByte(0x7e); // ~
stream.WriteByte(0x6d); // m
stream.WriteByte(0x7e); // ~
stream.Write(length, 0, length.Length);
stream.WriteByte(0x7e); // ~
stream.WriteByte(0x6d); // m
stream.WriteByte(0x7e); // ~
stream.Write(msg, 0, msg.Length);
stream.WriteByte(0xff);
stream.Flush();

} catch (IOException ioe) {
throw new SocketIoClientException("error while sending text data", ioe);
}
}
}


public void handleReceiverError() {
try {
if (connected) {
close();
}
} catch (SocketIoClientException) {
//Console.WriteLine(wse.StackTrace);
}
}


public void close() {
lock (lockThis) {
if (!connected) {
//Console.WriteLine("Trying to close, but not connected");
return;
}

if (receiver.isRunning()) {
receiver.stopit();
}

sendCloseHandshake();

closeStreams();

eventHandler.OnClose();
}
}


private void sendCloseHandshake() {
lock (lockThis) {
if (!connected) {
throw new SocketIoClientException("error while sending close handshake: not connected");
}

try {
stream.WriteByte(0x00);
stream.Flush();
}
catch (IOException ioe) {
throw new SocketIoClientException("error while sending close handshake", ioe);
}

connected = false;
}
}


private TcpClient createSocket() {
string scheme = url.Scheme;
string host = url.Host;
int port = url.Port;

TcpClient socket;

if (scheme != null && scheme.Equals("ws")) {
if (port == -1) {
port = 80;
}

try {
socket = new TcpClient(host, port);
} catch (IOException ioe) {
throw new SocketIoClientException("error while creating socket to " + url, ioe);
}
} else if (scheme != null && scheme.Equals("wss")) {
throw new SocketIoClientException("Secure Sockets not implemented");
} else {
throw new SocketIoClientException("unsupported protocol: " + scheme);
}

return socket;
}


private void closeStreams() {
try {
input.Close();
stream.Close();
socket.Close();
} catch (IOException ioe) {
throw new SocketIoClientException("error while closing SocketIoClient connection: ", ioe);
}
}
}
12 changes: 12 additions & 0 deletions lib/SocketIoClientException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

public class SocketIoClientException : Exception {
#pragma warning disable 0414
private static long serialVersionUID = 1L;
#pragma warning restore 0414

public SocketIoClientException(string message) : base(message) {}


public SocketIoClientException(string message, Exception t) : base(message, t) {}
}
Loading

0 comments on commit b0eeebc

Please sign in to comment.