• iris websocket服务端

package mainimport ("fmt""github.com/kataras/iris""github.com/kataras/iris/websocket"
)func main() {app := iris.New()app.Get("/", func(ctx iris.Context) {ctx.ServeFile("websockets.html", false) // second parameter: enable gzip?})setupWebsocket(app)// x2// http://localhost:8080// http://localhost:8080// write something, press submit, see the result.app.Run(iris.Addr(":8080"))
}func setupWebsocket(app *iris.Application) {// create our echo websocket serverws := websocket.New(websocket.Config{ReadBufferSize:  1024,WriteBufferSize: 1024,})ws.OnConnection(handleConnection)// register the server on an endpoint.// see the inline javascript code in the websockets.html,// this endpoint is used to connect to the server.app.Get("/echo", ws.Handler())// serve the javascript built'n client-side library,// see websockets.html script tags, this path is used.app.Any("/iris-ws.js", websocket.ClientHandler())
}func handleConnection(c websocket.Connection) {// Read events from browserc.On("chat", func(msg string) {// Print the message to the console, c.Context() is the iris's http context.fmt.Printf("%s sent: %s\n", c.Context().RemoteAddr(), msg)// Write message back to the client message owner with:// c.Emit("chat", msg)// Write message to all except this client with:c.To(websocket.Broadcast).Emit("chat", msg)})
}
  • 客户端html页面websockets.html

<!-- the message's input -->
<input id="input" type="text" /><!-- when clicked then an iris websocket event will be sent to the server,
at this example we registered the 'chat' -->
<button onclick="send()">Send</button><!-- the messages will be shown here -->
<pre id="output"></pre>
<!-- import the iris client-side library for browser-->
<script src="/iris-ws.js"></script><script>var scheme = document.location.protocol == "https:" ? "wss" : "ws";var port = document.location.port ? (":" + document.location.port) : "";// see app.Get("/echo", ws.Handler()) on main.govar wsURL = scheme + "://" + document.location.hostname + port+"/echo";var input = document.getElementById("input");var output = document.getElementById("output");// Ws comes from the auto-served '/iris-ws.js'var socket = new Ws(wsURL)socket.OnConnect(function () {output.innerHTML += "Status: Connected\n";});socket.OnDisconnect(function () {output.innerHTML += "Status: Disconnected\n";});// read events from the serversocket.On("chat", function (msg) {addMessage(msg);});function send() {addMessage("Me: " + input.value); // write ourselvessocket.Emit("chat", input.value); // send chat event data to the websocket serverinput.value = ""; // clear the input}function addMessage(msg) {output.innerHTML += msg + "\n";}
</script>
  • 启动服务端后,打开三个localhost:8080页面

1号页面输入我是1号

2号页面输入我是2号

3号页面输入我是3号

三个页面分别显示如下:

代码来自官方https://github.com/kataras/iris

【GO】13.Iris WebSocket 实例相关推荐

  1. Java Websocket实例【服务端与客户端实现全双工通讯】

    Java Websocket实例[服务端与客户端实现全双工通讯] 现很多网站为了实现即时通讯,所用的技术都是轮询(polling).轮询是在特定的的时间间隔(如每1秒),由浏览器对服务器发 出HTTP ...

  2. c#--Fleck WebSocket使用 (C#版Websocket实例)

    推荐几篇文章: WebSocket 实战 C#版Websocket实例 C#工作总结(一):Fleck的WebSocket使用 1.服务端代码 using Fleck; using System; u ...

  3. php swoole实例,Swoole WebSocket实例

    Swoole WebSocket实例 Swoole WebSocket介绍 swoole-1.7.9 增加了内置的websocket服务器支持,通过几行PHP代码就可以写出一个异步非阻塞多进程的Web ...

  4. JavaScript websocket 实例

    实例: 即时通讯聊天室demo可以打开两个页面互相发送消息查看. websocket.js /* 判断浏览器是否内置了websocket */if ('WebSocket' in window) {w ...

  5. gorilla websocket 实例

    以下内容转自自  https://blog.csdn.net/lanyang123456/article/details/83215220 WebSocket协议是基于TCP的一种新的网络协议.它实现 ...

  6. Golang Iris Websocket 跨域问题

    问题描述 在尝试使用iris中的websocket搭建一个实时通讯聊天的demo时,出现一个Upgrade Error错误,一个跨域问题,网上说要修改golang中的websocket upgrade ...

  7. Springboot集成websocket实例

    一.简介 WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数 ...

  8. 图解OpenLayers-2.13.1入门实例

    1 下载 http://www.openlayers.cn/forum.php?mod=viewthread&tid=171 解压后有如下内容: 2 实例1 把img, theme 文件夹,O ...

  9. 13道Python3实例

    1.Python数字求和 #用户输入数字 num1 = input("输入第一个数:") num2 = input("输入第二个数:") #求和 sum= fl ...

最新文章

  1. 超级账本(Hyperledger Fabric)之权限管理浅析
  2. java 图像采集_JAVA B/S通过摄像头采集图片信息解决方案。
  3. 网易游戏2016实习生招聘笔试题目--推箱子
  4. 原创 MySQL探秘(八):基于Redo Log和Undo Log的MySQL崩溃恢复流程(一致性)
  5. 7.4.6 核PCA
  6. 关于三层交换机的智能流技术
  7. [OpenBMC] 快速上手OpenBMC的Redfish
  8. 计算机管理员密码忘记了怎么恢复,新版tplink(tplogin.cn)管理员密码忘记了怎么办?...
  9. 【机器学习PAI实践十一】机器学习PAI为你自动写歌词,妈妈再也不用担心我的freestyle了(提供数据、代码
  10. Google adsense帐户被封到解封全过程
  11. ybc_art打印艺术字
  12. 一些大任务SQL的优化方案
  13. 通过ROS进行多机协同控制
  14. rs_D455相机内外参标定+imu联合标定
  15. git如何将本地分支关联到远程分支
  16. 来了老弟,表格的渲染
  17. 如何采集QQ群中所有成员QQ号码
  18. mac terminal终端打开指定文件夹 当前文件夹打开terminal
  19. 我们在6月缅怀——上半年那些被卫星“刻录”的记忆、影像
  20. 云呐|固定资产管理系统的主要功能有哪些

热门文章

  1. 2015阿里看雪移动安全挑战赛-第二题
  2. linux shell 输出红色,Bash Shell怎么打印各种颜色
  3. 晚睡原来是一种病——拖延症
  4. 数据库系统原理选择题总结
  5. 提取DC综合report_constrain all violator中big neg slack
  6. PyCharm SyntaxError: Non-UTF-8 code starting with \xbb 处理
  7. JAVA-JDBC事务基础概念及测试方法
  8. 7.10网站数据统计
  9. 共享图书系统 图书借阅小程序系统开发心得体会 共享图书系统开发
  10. matebook13写JAVA_大上黑白电子墨水屏显示器Paperlike HD-FT使用体验