SocketIO for Unity - v1.0.0
(Tested with Unity 5.5.2f)
This plugin allows you to integrate your Unity game with Socket.IO back-end It implements the protocol described at socket.io-protocol github repo. ( https://github.com/automattic/socket.io-protocol )
While connected, Socket.IO run on it's own thread to avoid blocking the main thread. Events are queued and dispatched on the next frame they are received.
In order to start using Socket.IO in your project you need to:
- Drag the SocketIO prefab from SocketIO/Prefab/ to your scene.
- Configure the url where your Socket.IO server is listening.
- Toggle the autoconnect flag if you want it to be always running.
- That's it! You can now start using Socket.IO in your game.
- Obtaining the Socket.IO component reference
GameObject go = GameObject.Find("SocketIO");
SocketIOComponent socket = go.GetComponent<SocketIOComponent>();
Bear in mind that using GameObject.Find might be pretty expensive, you
might want to store that reference in a variable for later use.
- Receiving events
Using the socket reference you can receive custom events
public void Start(){
socket.On("boop", TestBoop);
}
public void TestBoop(SocketIOEvent e){
Debug.Log(string.Format("[name: {0}, data: {1}]", e.name, e.data));
}
Also, you can also use lambda expresions as callbacks
socket.On("boop", (SocketIOEvent e) => {
Debug.Log(string.Format("[name: {0}, data: {1}]", e.name, e.data));
});
- Sending events
Besides listening to Socket.IO events or your custom events, you can
use send information to Socket.IO server using the Emit method.
a) Sending plain messages
socket.Emit("user:login");
b) you can pass object.
class Foo{ string data;}
socket.Emit("emitData", new Foo());
c) Sending additional data
Dictionary<string, string> data = new Dictionary<string, string>();
data["email"] = "some@email.com";
data["pass"] = Encrypt("1234");
socket.Emit("user:login", new JSONObject(data));
d) Sometimes, you might want to get a callback when the client confirmed
the message reception. To do this, simply pass a function as the last
parameter of .Emit()
socket.Emit("user:login", OnLogin);
socket.Emit("user:login", new JSONObject(data), OnLogin);
- Obtaining current socket id (socket.sid)
public void Start(){
socket.On("open", OnSocketOpen);
}
public void OnSocketOpen(SocketIOEvent ev){
Debug.Log("updated socket id " + socket.sid);
}
-
Namespace Support Not implemented yet!
-
Binary Events Not implemented yet!
This package also includes a minimalist test that you might want to use to verify that you have setup your environment properly.
-
Navigate to the server directory cd PATH/TO/PROJECT/Assets/SocketIO/Server
-
Unzip the server code outside Unity folder unzip beep.js.zip -d /tmp/socketio
-
Go to the destination folder where the server code was extracted cd /tmp/socketio
-
Install Socket.IO server package npm install socket.io
-
(Optional) Enable debug mode Windows: set DEBUG=* Mac: export DEBUG=*
-
Run test server node ./beep.js
-
Open the test scene SocketIO/Scenes/SocketIOTest
-
Run the scene. Some debug message will be printed to Unity console.
-
Open SocketIO/Scripts/Test/TestSocketIO.cs to check what's going on.
This is the first release of the plugin, so error may appear. In order to track them, I've included some debug code that it's only compiled when you uncomment some flags.
In order to enable debug messages you might:
- Navigate to SocketIO/Scripts/SocketIO
- Open files Decoder.cs, Encoder.cs and SocketIOComponent.cs
- Uncomment the following line #define SOCKET_IO_DEBUG
- Run your game again. This time messages sent and received will be logged to Unity console. Hopefully you might find some lead to the source of your problems / bugs.
- Remember to comment it again after you used it.
SocketIO for Unity is provided under The MIT License