基于golang实现的分布式聊天系统,支持一对一聊天,聊天室等功能。为了测试方便发送消息数据暂未存入数据库,后期会加入数据库,也可自行加入数据库,方便永久存储聊天内容,以及支持消息必达等功能。php
github.com/go-redis/redis github.com/gin-gonic/gin github.com/gorilla/websocket github.com/smallnest/rpcx
包说明:nginx
redis :用于缓存ws服务器信息,用心跳形式维护ws服务器信息。git
gin:实现web服务github
websocket: 实现websocket协议golang
rpcx:服务器建rpc通讯web
git clone https://github.com/guyan0319/go-websocket.git
注:这里代码版本控制使用go modulesredis
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...segmentfault
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...