社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
基于golang实现的分布式聊天系统,支持一对一聊天,聊天室等功能。为了测试方便发送消息数据暂未存入数据库,后期会加入数据库,也可自行加入数据库,方便永久存储聊天内容,以及支持消息必达等功能。
github.com/go-redis/redis
github.com/gin-gonic/gin
github.com/gorilla/websocket
github.com/smallnest/rpcx
包说明:
redis :用于缓存ws服务器信息,用心跳形式维护ws服务器信息。
gin:实现web服务
websocket: 实现websocket协议
rpcx:服务器建rpc通信
git clone https://github.com/guyan0319/go-websocket.git
注:这里代码版本控制使用go modules
go run main.go
upstream go-http
{
server 127.0.0.1:8282 weight=1 max_fails=2 fail_timeout=10s;
keepalive 16;
}
upstream go-ws
{
server 127.0.0.1:8089 weight=1 max_fails=2 fail_timeout=10s;
keepalive 16;
}
server {
listen 80;
server_name ws.test;
root "D:/phpstudy/WWW/";
location /ws {
proxy_set_header Host $host;
proxy_pass http://go-ws;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Connection "";
proxy_redirect off;
proxy_intercept_errors on;
client_max_body_size 10m;
}
location /
{
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://go-http;
}
}
浏览器打开两个窗口访问
http://ws.test/home/index?uid...
http://ws.test/home/index?uid...
浏览器打开两个窗口访问
http://ws.test/home/room?uid=1
http://ws.test/home/room?uid=2
github.com/gorilla/websocket
https://my.oschina.net/u/4231...
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!