Golang使用websocket

本文介绍了在Golang中使用WebSocket时,针对浏览器和非浏览器客户端的区别。Handler适合处理带有Origin头的浏览器客户端,而Server则允许接受不包含Origin头的非浏览器客户端连接。文中通过代码示例展示了如何分别使用Handler和Server来建立WebSocket连接,并讨论了Go web服务如何处理用户请求。
摘要由CSDN通过智能技术生成

1、在Google官方维护的code.google.com\p\go.net\websocket包中的server.go文件中,曾经有这么一段描述:

   Handler is a simple interface to a WebSocket browser client.  It checks if Origin header is 
   valid URL by default. You might want to verify websocket.Conn.Config().Origin in the func.  
   If you use Server instead of Handler, you could call websocket.Origin and  check the origin 
   in your Handshake func. So, if you want to accept non-browser client, which doesn't send 
   Origin header, you could use Server. that doesn't check origin in its Handshake.


这里说到,Handler是一个针对Websocket浏览器客户端的简单接口,默认情况下,Handler会检查Http请求的头文件的Origin是否是一个有效的值。最后说到,如果你想接收一个并不带有Origin字段信息的非浏览器客户端发送的websocket请求,你应该使用Server,使用Server不会在Websocket握手时对Origin进行检查。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值