• Unity 连接WebSocket


    Unity 连接ws

    using System;

    using System.Net.WebSockets;
    using System.Text;
    using System.Threading;
    using UnityEngine;
     
    public class NewBehaviourScript : MonoBehaviour
    {
        private void Start()
        {
            WebSocket();
        }
     
        public async void WebSocket()
        {
            try
            {
                ClientWebSocket ws = new ClientWebSocket();
                CancellationToken ct = new CancellationToken();
                Uri url = new Uri("ws://127.0.0.1:8080/v1/test/");
                await ws.ConnectAsync(url, ct);
                await ws.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes("hello")), WebSocketMessageType.Binary, true, ct); //发送数据
                while (true)
                {
                    var result = new byte[1024];
                    await ws.ReceiveAsync(new ArraySegment<byte>(result), new CancellationToken());//接受数据
                    var str = Encoding.UTF8.GetString(result, 0, result.Length);
                    Debug.Log(str);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
     
    }
  • 相关阅读:
    OpenStack Pike超详细搭建安装文档 LinuxBridge版
    OpenStack Ocata 超详细搭建文档
    此博客关闭,转到 https://www.justbio.wang/
    面向对象03 封装 反射 内置方法
    面向对象02 继承
    面向对象01
    awk使用说明
    MySQL优化聊两句
    Redis分布式集群几点说道
    Nginx最大客户连接数算法一些遐想
  • 原文地址:https://www.cnblogs.com/zhao907/p/13398107.html
Copyright © 2020-2023  润新知