基於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...