示例#1
0
    async void Start()
    {
        websocket = new WebSocket(SERVER_URL);

        websocket.OnOpen += () =>
        {
            Debug1.Log("Connection open!");
        };

        websocket.OnError += (e) =>
        {
            Debug1.Log("Error! " + e);
        };

        websocket.OnClose += (e) =>
        {
            Debug1.Log("Connection closed!");
        };

        websocket.OnMessage += (bytes) =>
        {
            var message = System.Text.Encoding.UTF8.GetString(bytes);

            OnlineData sync = JsonUtility.FromJson <OnlineData>(message);

            if (!dic.ContainsKey(sync.id))
            {
                GameObject go   = Instantiate(player, sync.desirePos, Quaternion.identity);
                var        comp = go.GetComponent <SyncPosition>().data;
                comp.id        = sync.id;
                comp.desirePos = sync.desirePos;
                comp.state     = sync.state;
                comp.speed     = sync.speed;

                go.transform.GetChild(0).GetComponent <TextMeshPro>().text = sync.id;

                dic.Add(sync.id, go.GetComponent <SyncPosition>());
            }
            else
            {
                if (sync.id != playerid)
                {
                    dic[sync.id].data.desirePos = sync.desirePos;
                    dic[sync.id].data.state     = sync.state;
                }
            }
        };

        InvokeRepeating("SendWebSocketMessage", 0.0f, 0.3f);

        await websocket.Connect();
    }
示例#2
0
    void Start()
    {
        Debug1.Log("starting web socket sharp");

        using (var ws = new WebSocket("wss://fishtankserver.herokuapp.com/ws"))
        {
            ws.OnMessage += (sender, e) =>
                            Debug1.Log("Message receive: " + e.Data);
            //Console.WriteLine("Laputa says: " + e.Data);

            ws.Connect();
            ws.Send("BALUS");
        }
    }