npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

com.ellyality.network.socketio

v1.0.2

Published

Sqlite database SDK

Downloads

23

Readme

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 and/or 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 paste 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.Emit("eventName",(response)=>{
    string text = response.GetValue<string>();
    print(text);
}, 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/Disconnecting:

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.