首先我们需要一个websocket服务器,之前的博文中有做

Tomcat架设简单Websocket服务器

用的时候打开就行了,先不管它

Unity中新建场景

建UI(UGUI)

有一个连接按钮Button

一个信息输入框InputField

一个发送按钮Button

一个断开按钮Button

一个消息显示框Text

场景中建一个GameObject,在上面加个脚本,就叫WSMgr好了

用到了BestHTTP这个插件

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using BestHTTP;

using BestHTTP.WebSocket;

using System;

using BestHTTP.Examples;

using UnityEngine.UI;

using System.Text;

public class WSMgr : MonoBehaviour {

//public string url = "ws://localhost:8080/web1/websocket";

public string url = "ws://localhost:8080/web1/ws";

public InputField msg;

public Text console;

private WebSocket webSocket;

private void Start()

{

init();

}

private void init()

{

webSocket = new WebSocket(new Uri(url));

webSocket.OnOpen += OnOpen;

webSocket.OnMessage += OnMessageReceived;

webSocket.OnError += OnError;

webSocket.OnClosed += OnClosed;

}

private void antiInit()

{

webSocket.OnOpen = null;

webSocket.OnMessage = null;

webSocket.OnError = null;

webSocket.OnClosed = null;

webSocket = null;

}

private void setConsoleMsg(string msg)

{

console.text = "Message: " + msg;

}

public void Connect()

{

webSocket.Open();

}

private byte[] getBytes(string message)

{

byte[] buffer = Encoding.Default.GetBytes(message);

return buffer;

}

public void Send()

{

webSocket.Send(msg.text);

}

public void Send(string str)

{

webSocket.Send(str);

}

public void Close()

{

webSocket.Close();

}

#region WebSocket Event Handlers

///

/// Called when the web socket is open, and we are ready to send and receive data

///

void OnOpen(WebSocket ws)

{

Debug.Log("connected");

setConsoleMsg("Connected");

}

///

/// Called when we received a text message from the server

///

void OnMessageReceived(WebSocket ws, string message)

{

Debug.Log(message);

setConsoleMsg(message);

}

///

/// Called when the web socket closed

///

void OnClosed(WebSocket ws, UInt16 code, string message)

{

Debug.Log(message);

setConsoleMsg(message);

antiInit();

init();

}

private void OnDestroy()

{

if(webSocket!=null && webSocket.IsOpen)

{

webSocket.Close();

antiInit();

}

}

///

/// Called when an error occured on client side

///

void OnError(WebSocket ws, Exception ex)

{

string errorMsg = string.Empty;

#if !UNITY_WEBGL || UNITY_EDITOR

if (ws.InternalRequest.Response != null)

errorMsg = string.Format("Status Code from Server: {0} and Message: {1}", ws.InternalRequest.Response.StatusCode, ws.InternalRequest.Response.Message);

#endif

Debug.Log(errorMsg);

setConsoleMsg(errorMsg);

antiInit();

init();

}

#endregion

}

Connect Send Close 三个方法对应的就是三个按钮的功能

OnOpen OnMessage OnError OnClose这四个一看就是Websocket的四个事件对应的方法

首先在Start方法中init()

点击Connect,连接

在输入框中输入,点击Send就发送

点击Close断开连接

底部的Text中会显示消息

同样,Tomcat服务端也有显示

发布WebGL测试可用

websocket python unity_Unity中Websocket的简单使用相关推荐

  1. websocket python unity_Unity 连接WebSocket(ws://)服务器

    Unity 连接ws,不用任何插件,忙活了一天终于搞定了,一直连接不上,原来是没有添加header, 代码比较简单,直接贴出来普度众生 using System; using System.Net.W ...

  2. python list中的sort()简单用法与lambda的使用

    序列类型分三种:元组,字符串,列表 list 列表属于序列类型,于元组不同的是 元组类型一旦创建不可修改 使用()或者tuple()创建,而列表类型创建后可以被随意修改 使用[]或list()创建 s ...

  3. python socketio_flask-socketio实现WebSocket的方法

    [flask-socektio] 之前不知道在哪个场合下提到过如何从web后台向前台推送消息.听闻了反向ajax技术这种模式之后,大呼神奇,试了一下之后发现也确实可以用.不过,反向ajax的代价也很明 ...

  4. 基于python以及AIUI WebSocket,WeChatPYAPI实现的微信聊天机器人

    基于python以及AIUI WebSocket,WeChatPYAPI实现的微信聊天机器人 做此文的目的首先是学习Markdown的用法哈哈哈哈,其实也是记录自己学习的一个过程. 以后我也会将自己在 ...

  5. python websocket_python 模拟websocket通信

    以前,很多网站使用轮询实现推送技术.轮询是在特定的的时间间隔(比如1秒),由浏览器对服务器发出HTTP request,然后由服务器返回最新的数据给浏览器.轮询的缺点很明显,浏览器需要不断的向服务器发 ...

  6. 架构师讲解Java中websocket的应用

    这篇文章主要来介绍一下在java项目中,特别是java web项目中websocket的应用. 场景:我做了一个商城系统,跟大多数商城系统,分为客户端和后台,客户端供客户浏览,下单,购买,后台主要管理 ...

  7. java netty wss_netty中websocket, wss

    websocket 直接使用SpringBoot+Netty来支持WebSocket,并且需要支持wss,其需要注意事项有以下: wss支持 websocket请求路径中带参数 针对第一个问题:wss ...

  8. WebSocket——SpringBoot+Vue3+TS+SockJS+STOMP简单连接使用

    WebSocket--SpringBoot+Vue3+TS+SockJS+STOMP简单连接使用 本文视频以及相关资源 关于WebSocket 文档 什么时候使用WebSocket WebSocket ...

  9. 【Web通信】WebSocket详解:WebSocket是什么?如何使用WebSocket?在Vue中封装WebSocket(心跳监测)。nginx配置websocket。

    一.WebSocket相关定义 1. WebSocket定义 WebSocket 是一种基于TCP的全双工通信协议,它提供了一种在浏览器和服务器之间建立持久连接来交换数据的方法.数据可以作为" ...

最新文章

  1. canvas-绘制矩形-读书笔记
  2. Git学习系列(二)创建本地仓库及文件操作
  3. SD-WAN成本节省取决于基础WAN技术
  4. 百度地图手机和电脑不一致_如何解决电脑显色和印刷色不一致的问题
  5. django和flask用MD5加密密码
  6. getParameter和getAttribute的区别
  7. 我们为什么活得这么累
  8. 数据挖掘之自然语言处理
  9. 代号斗罗显示服务器暂未开放,代号斗罗手游
  10. JVM本地方法栈及native方法
  11. Excel VBA教程之如何在功能区中显示 Excel 开发人员选项卡,启用vba(教程含源码)
  12. 通俗易懂理解几何光学(三)平面与平面系统
  13. 免费下载380套大型商业源码
  14. 0201电脑桌的制作过程(使用3DsMAX2016)
  15. 何为企业?何以“大而能用,大而有当”?|一点财经
  16. python区间中的数字统计
  17. vant-ui的官方入口
  18. Android 画椭圆
  19. springboot properties
  20. Android 获取设备唯一标识

热门文章

  1. linux sshd进程起不来,linux sshd服务异常
  2. ubuntu12.04 java配置_Ubuntu 12.04 中安装和配置 Java JDK
  3. Springcloud 高效率本地加Redis双级缓存
  4. leetcode题解50-Pow(x,n)
  5. 【redis】redis应用场景,缓存的各种问题
  6. MapUtils常用方法
  7. Tarjan-有向图
  8. Linux的通信命令
  9. 微信小程序四(设置底部导航)
  10. 在 Perl 中使用 Getopt::Long 模块来接收用户命令行参数