Socket.IO for Unity的使用

Drag the SocketIO prefab from SocketIO/Prefab/ to your scene.
  2. Configure the url where your Socket.IO server is listening.
  3. Toggle the autoconnect flag if you want it to be always running.
  4. That's it! You can now start using Socket.IO in your game.

首先我们需要把SocketIO/Prefab里面的SocketIO拖入到场景中,然后
    private SocketIOComponent socket;
  在start 方法里:
      GameObject go = GameObject.Find("SocketIO");
        socket = go.GetComponent<SocketIOComponent>();
还有就是发送的方法:

1、socket.Emit("user:login");//发送一个方法名或者是string类型的消息
2、Dictionary<string, string> data = new Dictionary<string, string>();
       data["email"] = "[email protected]";
       data["pass"] = Encrypt("1234");
       socket.Emit("user:login", new JSONObject(data));//需要一个string类型方法名或者代表其他,然后发送一个json数据信息
3、socket.Emit("user:login", new JSONObject(data), OnLogin); //需要一个string类型方法名或者代表其他,然后发送一个json数据信息,然后写一个回掉函数

还有就是接收消息方法:socket.On("boop", TestBoop);//相当于回掉函数吧,我觉得是处理返回的数据

数据的处理方法:
public void TestMatchOpen(SocketIOEvent e)
    {
        // string s=e.data["open"]
        JsonData data = JsonMapper.ToObject(e.data.ToString());
    }

暂时了解到这些。


猜你喜欢

转载自blog.csdn.net/gao7009/article/details/79928976