利用Go语言搭建WebSocket服务端方法示例
作者:小谈博客 时间:2024-05-08 10:14:43
Go 搭建一个简单 WebSocket 服务端代码例子 test.go, 如下:
package main
import (
"fmt"
"log"
"net/http"
"golang.org/x/net/websocket"
)
func Echo(ws *websocket.Conn) {
var err error
for {
var reply string
if err = websocket.Message.Receive(ws, &reply); err != nil {
fmt.Println("Can't receive")
break
}
fmt.Println("Received back from client: " + reply)
msg := "Received: " + reply
fmt.Println("Sending to client: " + msg)
if err = websocket.Message.Send(ws, msg); err != nil {
fmt.Println("Can't send")
break
}
}
}
func main() {
http.Handle("/", websocket.Handler(Echo))
if err := http.ListenAndServe(":1234", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
运行服务端:
go run test.go
在 Firefox 或 Chrome 控制台,可以用以下代码发起客户端的 WebSocket 连接:
var wsServer = 'ws://localhost:1234';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {
console.log("Connected to WebSocket server.");
};
websocket.onclose = function (evt) {
console.log("Disconnected");
};
websocket.onmessage = function (evt) {
console.log('Retrieved data from server: ' + evt.data);
};
websocket.onerror = function (evt, e) {
console.log('Error occured: ' + evt.data);
};
执行以上代码,得到输出日志:Connected to WebSocket server,表示 WebSocket 连接建立成功。
使用如下代码可以发送数据,如:
websocket.send(2323)
并且马上得到服务端返回的内容,这里是原样返回。
服务端日志如下:
这就使用 Go 搭建了一个简单的 WebSocket 服务端。
来源:https://blog.tanteng.me/2017/02/go-websocket-demo/
标签:GO语言,websocket,服务端
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
electron-builder打包配置详解
2024-04-17 10:24:54
sql中count或sum为条件的查询示例(sql查询count)
2024-01-16 04:05:02
![](https://img.aspxhome.com/file/2023/2/96162_0s.jpg)
理解CSS3线性渐变
2010-03-28 13:42:00
![](https://img.aspxhome.com/file/UploadPic/20103/28/01-27s.jpg)
pandas groupby分组对象的组内排序解决方案
2023-06-15 18:34:58
Python安装xarray库读取.nc文件的详细步骤
2023-10-15 11:59:37
![](https://img.aspxhome.com/file/2023/4/133324_0s.png)
Python实现的FTP通信客户端与服务器端功能示例
2023-10-02 21:36:01
![](https://img.aspxhome.com/file/2023/9/63189_0s.png)
Python采集某度贴吧排行榜实战示例
2022-03-08 21:07:17
![](https://img.aspxhome.com/file/2023/9/121029_0s.png)
解决python虚拟环境切换无效的问题
2023-02-01 14:37:50
![](https://img.aspxhome.com/file/2023/0/77860_0s.jpg)
mysql 8.0.12安装配置方法图文教程(Windows版)
2024-01-13 14:57:40
![](https://img.aspxhome.com/file/2023/9/111369_0s.png)
Python类class参数self原理解析
2023-12-23 11:51:35
![](https://img.aspxhome.com/file/2023/7/112487_0s.png)
Python 文本文件与csv文件的读取与写入
2021-02-10 09:57:56
Python list和str互转的实现示例
2022-12-26 13:26:56
浅谈JavaScript中的“!!”作用
2024-05-05 09:04:53
PHP判断数组是否为空的常用方法(五种方法)
2024-05-11 09:25:53
python实现用户答题功能
2022-01-05 21:38:02
![](https://img.aspxhome.com/file/2023/5/93555_0s.png)
Python二叉树初识(新手也秒懂!)
2022-10-09 13:03:36
![](https://img.aspxhome.com/file/2023/0/133570_0s.jpg)
对python中的乘法dot和对应分量相乘multiply详解
2021-01-14 15:54:58
Laravel 微信小程序后端实现用户登录的示例代码
2024-06-05 15:40:52
![](https://img.aspxhome.com/file/2023/7/122847_0s.jpg)
Python如何测试stdout输出
2023-10-22 23:59:56
pygame 键盘事件的实践
2023-09-29 18:56:10
![](https://img.aspxhome.com/file/2023/5/107585_0s.png)