{"id":15043716,"url":"https://github.com/itisnajim/socketiounity","last_synced_at":"2025-04-04T22:03:24.396Z","repository":{"id":37340051,"uuid":"422950454","full_name":"itisnajim/SocketIOUnity","owner":"itisnajim","description":"A Wrapper for socket.io-client-csharp to work with Unity.","archived":false,"fork":false,"pushed_at":"2024-05-30T01:09:35.000Z","size":4255,"stargazers_count":454,"open_issues_count":14,"forks_count":80,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-04T22:03:17.033Z","etag":null,"topics":["connection","events","io","multiplayer","realtime","socket","socket-io","socketio","tcp","unity","unity3d","websocket"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/itisnajim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-30T17:41:10.000Z","updated_at":"2025-04-03T19:01:26.000Z","dependencies_parsed_at":"2024-09-25T02:02:07.724Z","dependency_job_id":null,"html_url":"https://github.com/itisnajim/SocketIOUnity","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itisnajim%2FSocketIOUnity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itisnajim%2FSocketIOUnity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itisnajim%2FSocketIOUnity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itisnajim%2FSocketIOUnity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/itisnajim","download_url":"https://codeload.github.com/itisnajim/SocketIOUnity/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247256104,"owners_count":20909240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["connection","events","io","multiplayer","realtime","socket","socket-io","socketio","tcp","unity","unity3d","websocket"],"created_at":"2024-09-24T20:49:29.167Z","updated_at":"2025-04-04T22:03:24.367Z","avatar_url":"https://github.com/itisnajim.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SocketIOUnity\n\n## Description\nA Wrapper for [socket.io-client-csharp](https://github.com/doghappy/socket.io-client-csharp) to work with Unity,\nSupports socket.io server v2/v3/v4, and has implemented http polling and websocket. \n\n## Give a Star! ⭐\nFeel free to request an issue on github if you find bugs or request a new feature. \nIf you find this useful, please give it a star to show your support for this project.\n\n## Supported Platforms\n💻 PC/Mac, 🍎 iOS, 🤖 Android\n\nOther platforms (including the Editor) have not been tested and/or may not work!\n\n## Example\n![Example](https://github.com/itisnajim/SocketIOUnity/blob/main/Samples~/example.gif?raw=true)\n\n## Installation\nCopy this url: \n\n```https://github.com/itisnajim/SocketIOUnity.git```\nthen in Unity open Window -\u003e Package Manager -\u003e and click (+) add package from git URL... and paste it there.\n\n\n## Usage\nCheck the 'Samples~' folder and [socket.io-client-csharp](https://github.com/doghappy/socket.io-client-csharp) repo for more usage info.\n\n### Initiation: \nYou may want to put the script on the Camera Object or using ```DontDestroyOnLoad``` to keep the socket alive between scenes!\n```csharp\nvar uri = new Uri(\"https://www.example.com\");\nsocket = new SocketIOUnity(uri, new SocketIOOptions\n{\n    Query = new Dictionary\u003cstring, string\u003e\n        {\n            {\"token\", \"UNITY\" }\n        }\n    ,\n    Transport = SocketIOClient.Transport.TransportProtocol.WebSocket\n});\n```\n\n### JsonSerializer:\nThe library uses System.Text.Json to serialize and deserialize json by default, may [won't work in the current il2cpp](https://forum.unity.com/threads/please-add-system-text-json-support.1000369/).\nYou can use Newtonsoft Json.Net instead:\n```csharp\nsocket.JsonSerializer = new NewtonsoftJsonSerializer();\n```\n\n### Emiting: \n```csharp\nsocket.Emit(\"eventName\");\nsocket.Emit(\"eventName\", \"Hello World\");\nsocket.Emit(\"eventName\", someObject);\n\nsocket.Emit(\"eventName\",(response)=\u003e{\n    string text = response.GetValue\u003cstring\u003e();\n    print(text);\n}, someObject);\n\nsocket.EmitStringAsJSON(\"eventName\", \"{\\\"foo\\\": \\\"bar\\\"}\");\nawait client.EmitAsync(\"hi\", \"socket.io\"); // Here you should make the method async\n```\n\n### Receiving: \n```csharp\nsocket.On(\"eventName\", (response) =\u003e\n{\n    /* Do Something with data! */\n    var obj = response.GetValue\u003cSomeClass\u003e();\n    ...\n});\n```\nif you want to play with unity game objects (eg: rotating an object) or saving data using PlayerPrefs system use this instead:\n```csharp\n// Set (unityThreadScope) the thread scope function where the code should run.\n// Options are: .Update, .LateUpdate or .FixedUpdate, default: UnityThreadScope.Update\nsocket.unityThreadScope = UnityThreadScope.Update; \n// \"spin\" is an example of an event name.\nsocket.OnUnityThread(\"spin\", (response) =\u003e\n{\n    objectToSpin.transform.Rotate(0, 45, 0);\n});\n```\nor: \n```csharp\nsocket.On(\"spin\", (response) =\u003e\n{\n    UnityThread.executeInUpdate(() =\u003e {\n        objectToSpin.transform.Rotate(0, 45, 0);\n    });\n    /* \n    or  \n    UnityThread.executeInLateUpdate(() =\u003e { ... });\n    or \n    UnityThread.executeInFixedUpdate(() =\u003e { ... });\n    */\n});\n```\n\n### Connecting/Disconnecting: \n```csharp\nsocket.Connect();\nawait socket.ConnectAsync();\n\nsocket.Disconnect();\nawait socket.DisconnectAsync();\n```\n\n## Server Example\n```javascript\nconst port = 11100;\nconst io = require('socket.io')();\nio.use((socket, next) =\u003e {\n    if (socket.handshake.query.token === \"UNITY\") {\n        next();\n    } else {\n        next(new Error(\"Authentication error\"));\n    }\n});\n\nio.on('connection', socket =\u003e {\n  socket.emit('connection', {date: new Date().getTime(), data: \"Hello Unity\"})\n\n  socket.on('hello', (data) =\u003e {\n    socket.emit('hello', {date: new Date().getTime(), data: data});\n  });\n\n  socket.on('spin', (data) =\u003e {\n    socket.emit('spin', {date: new Date().getTime()});\n  });\n\n  socket.on('class', (data) =\u003e {\n    socket.emit('class', {date: new Date().getTime(), data: data});\n  });\n});\n\nio.listen(port);\nconsole.log('listening on *:' + port);\n```\n\n## Acknowledgement\n[socket.io-client-csharp](https://github.com/doghappy/socket.io-client-csharp)\n\n[Socket.IO](https://github.com/socketio/socket.io)\n\n[System.Text.Json](https://docs.microsoft.com/en-us/dotnet/api/system.text.json)\n\n[Newtonsoft Json.NET](https://www.newtonsoft.com/json/help/html/Introduction.htm)\n\n[Unity Documentation](https://docs.unity.com)\n\n\n## Author\n\nitisnajim, itisnajim@gmail.com\n\n## License\n\nSocketIOUnity is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitisnajim%2Fsocketiounity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fitisnajim%2Fsocketiounity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitisnajim%2Fsocketiounity/lists"}