All Projects → itisnajim → SocketIOUnity

itisnajim / SocketIOUnity

Licence: MIT, Unknown licenses found Licenses found MIT LICENSE Unknown LICENSE.meta
A Wrapper for socket.io-client-csharp to work with Unity.

Programming Languages

C#
18002 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to SocketIOUnity

Bizsocket
异步socket,对一些业务场景做了支持
Stars: ✭ 469 (+579.71%)
Mutual labels:  socket, tcp, socket-io, socketio
Dodgem
A Simple Multiplayer Game, built with Mage Game Engine.
Stars: ✭ 12 (-82.61%)
Mutual labels:  socket, multiplayer, socket-io
socket-chat
This project will help you build a chat app by using the Socket IO library.
Stars: ✭ 36 (-47.83%)
Mutual labels:  socket, tcp, socket-io
echo-server
Echo Server is a Docker-ready, multi-scalable Node.js application used to host your own Socket.IO server for Laravel Broadcasting.
Stars: ✭ 32 (-53.62%)
Mutual labels:  socket, realtime, io
TweetMigration
A WebGL heatmap of global Twitter activity
Stars: ✭ 42 (-39.13%)
Mutual labels:  socket, socket-io, realtime
ddos
Simple dos attack utility
Stars: ✭ 36 (-47.83%)
Mutual labels:  socket, tcp, socket-io
Rltm.js
Easily swap realtime providers with a single code base
Stars: ✭ 106 (+53.62%)
Mutual labels:  socket, socket-io, realtime
Progress Bot
High-tech weaponized moe progress delivery bot for IRC, Discord, and web
Stars: ✭ 38 (-44.93%)
Mutual labels:  socket, socket-io, realtime
Oksocket
An blocking socket client for Android applications.
Stars: ✭ 2,359 (+3318.84%)
Mutual labels:  socket, tcp, socket-io
Node Decorators
node-decorators
Stars: ✭ 230 (+233.33%)
Mutual labels:  socket, socket-io, socketio
realtime-geolocation
Geolocation tracking app with Node.js, Socket.io, & AngularJS
Stars: ✭ 29 (-57.97%)
Mutual labels:  socket-io, realtime, socketio
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (-2.9%)
Mutual labels:  socket, tcp, socket-io
Phpsocket.io
A server side alternative implementation of socket.io in PHP based on workerman.
Stars: ✭ 2,026 (+2836.23%)
Mutual labels:  socket-io, realtime, socketio
Aaronvandenberg.nl
⚛️ Web Developers portfolio build with Gatsby.js & React.js
Stars: ✭ 98 (+42.03%)
Mutual labels:  socket-io, realtime, socketio
socket.io-client-core
High-Performance Socket.IO client in C#
Stars: ✭ 70 (+1.45%)
Mutual labels:  socket, socket-io, socketio
Socket
Non-blocking socket and TLS functionality for PHP based on Amp.
Stars: ✭ 122 (+76.81%)
Mutual labels:  socket, tcp, io
RRQMSocket
TouchSocket是.Net(包括 C# 、VB.Net、F#)的一个整合性的、超轻量级的网络通信框架。包含了 tcp、udp、ssl、http、websocket、rpc、jsonrpc、webapi、xmlrpc等一系列的通信模块。一键式解决 TCP 黏分包问题,udp大数据包分片组合问题等。使用协议模板,可快速实现「固定包头」、「固定长度」、「区间字符」等一系列的数据报文解析。
Stars: ✭ 286 (+314.49%)
Mutual labels:  socket, tcp, socket-io
KingNetwork
KingNetwork is an open source library to facilitate the creation and communication of clients and servers via TCP, UDP, WebSocket and RUDP sockets.
Stars: ✭ 78 (+13.04%)
Mutual labels:  socket, tcp, multiplayer
socket
Dazzle Async Socket
Stars: ✭ 19 (-72.46%)
Mutual labels:  socket, tcp
CleanArchitecture-SocketIO
CleanArchitecture with SocketIo 📡
Stars: ✭ 32 (-53.62%)
Mutual labels:  socket, socket-io

SocketIOUnity

Description

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.

Give a Star!

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.

Supported Platforms

💻 PC/Mac, 🍎 iOS, 🤖 Android

Other platforms(including the Editor) have not been tested or/and may not work!

Example

Example

Installation

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.

Usage

Check the 'Samples~' folder and socket.io-client-csharp repo for more usage info.

Initiation:

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
});

JsonSerializer:

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();

Emiting:

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

Receiving:

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(() => { ... });
    */
});

Connecting/Disconecting:

socket.Connect();
await socket.ConnectAsync();

socket.Disconnect();
await socket.DisconnectAsync();

Server Example

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);

Acknowledgement

socket.io-client-csharp

Socket.IO

System.Text.Json

Newtonsoft Json.NET

Unity Documentation

Author

itisnajim, [email protected]

License

SocketIOUnity is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].