
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
com.xrlab.aiotcore
Advanced tools
A Wrapper for socket.io-client-csharp to work with Unity, Supports socket.io server v2/v3/v4, and has implemented http polling and websocket.
Feel free to request an issue on github if you find bugs or request a new feature. If you find this useful, please give it a star to show your support for this project.
💻 PC/Mac, 🍎 iOS, 🤖 Android
Other platforms(including the Editor) have not been tested or/and may not work!
Copy this url:
https://github.com/itisnajim/SocketIOUnity.git
then in unity open Window -> Package Manager -> and click (+) add package from git URL... and past it there.
Check the 'Samples~' folder and socket.io-client-csharp repo for more usage info.
You may want to put the script on the Camera Object or using DontDestroyOnLoad
to keep the socket alive between scenes!
var uri = new Uri("https://www.example.com");
socket = new SocketIOUnity(uri, new SocketIOOptions
{
Query = new Dictionary<string, string>
{
{"token", "UNITY" }
}
,
Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
});
The library uses System.Text.Json to serialize and deserialize json by default, may won't work in the current il2cpp. You can use Newtonsoft Json.Net instead:
socket.JsonSerializer = new NewtonsoftJsonSerializer();
socket.Emit("eventName");
socket.Emit("eventName", "Hello World");
socket.Emit("eventName", someObject);
socket.EmitStringAsJSON("eventName", "{\"foo\": \"bar\"}");
await client.EmitAsync("hi", "socket.io"); // Here you should make the method async
socket.On("eventName", (response) =>
{
/* Do Something with data! */
var obj = response.GetValue<SomeClass>();
...
});
if you want to play with unity game objects (eg: rotating an object) or saving data using PlayerPrefs system use this instead:
// Set (unityThreadScope) the thread scope function where the code should run.
// Options are: .Update, .LateUpdate or .FixedUpdate, default: UnityThreadScope.Update
socket.unityThreadScope = UnityThreadScope.Update;
// "spin" is an example of an event name.
socket.OnUnityThread("spin", (response) =>
{
objectToSpin.transform.Rotate(0, 45, 0);
});
or:
socket.On("spin", (response) =>
{
UnityThread.executeInUpdate(() => {
objectToSpin.transform.Rotate(0, 45, 0);
});
/*
or
UnityThread.executeInLateUpdate(() => { ... });
or
UnityThread.executeInFixedUpdate(() => { ... });
*/
});
socket.Connect();
await socket.ConnectAsync();
socket.Disconnect();
await socket.DisconnectAsync();
const port = 11100;
const io = require('socket.io')();
io.use((socket, next) => {
if (socket.handshake.query.token === "UNITY") {
next();
} else {
next(new Error("Authentication error"));
}
});
io.on('connection', socket => {
socket.emit('connection', {date: new Date().getTime(), data: "Hello Unity"})
socket.on('hello', (data) => {
socket.emit('hello', {date: new Date().getTime(), data: data});
});
socket.on('spin', (data) => {
socket.emit('spin', {date: new Date().getTime()});
});
socket.on('class', (data) => {
socket.emit('class', {date: new Date().getTime(), data: data});
});
});
io.listen(port);
console.log('listening on *:' + port);
itisnajim, itisnajim@gmail.com
SocketIOUnity is available under the MIT license. See the LICENSE file for more info.
FAQs
For xrlab's cloud project(?
We found that com.xrlab.aiotcore demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.