# golang_websocket **Repository Path**: cx-a/golang_websocket ## Basic Information - **Project Name**: golang_websocket - **Description**: 后端向前端通过websocket推送,支持单个推送和组推送的简单demo - **Primary Language**: Go - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2025-01-09 - **Last Updated**: 2025-08-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # golang_websocket #### 介绍 后端向前端通过websocket推送,支持单个推送和组推送的简单demo 启动服务器:`这里输入代码` go run main.go 发送消息: # 发送给指定用户 curl -X POST -H "Content-Type: application/json" -d '{"user_code": "user1", "key": "user1_key", "server_password": "myServerPassword", "message": "Hello, user1!"}' http://localhost:8080/send-message # 发送给所有用户 curl -X POST -H "Content-Type: application/json" -d '{"server_password": "myServerPassword", "message": "Hello, all users!"}' http://localhost:8080/send-message windows打包linux运行的go文件 我们可以在终端中输入以下代码: ``` set CGO_ENABLED=0 set GOOS=linux set GOARCH=amd64 go build -o main-linux main.go ``` 然后就会生成main-linux的二进制可执行文件,然后我们就可以将main-linux放到服务器中的任一目录中,然后我们就可以执行以下命令运行。 ``` > chmod 777 main-linux > ./main-linux ``` 我们在终端以下代码查看: go env 我们可以看到,GOOS依然是windows,所以我们早linux运行windows的执行文件,自然无法正常执行。下面是正确的方法: ``` $env:GOOS="linux" go build -o main-linux main.go ``` 如果想让其在后台运行可以执行: ``` setsid ./main-linux ``` 如果报错:cc1.exe: all warnings being treated as errors 尝试输入: ``` set CGO_ENABLED=0 或者 $env:CGO_ENABLED=0 ``` 来源:https://blog.csdn.net/weixin_52534218/article/details/130058354 ``` set GOARCH=amd64 //设置目标可执行程序操作系统构架,包括 386,amd64,arm set GOOS=linux //设置可执行程序运行操作系统,支持 darwin,freebsd,linux,windows go build //打包 set GOOS=linux 不好使的话 直接 go env -w GOOS=linux 但是要记得改回来 ```