Skip to content

golang websocket cluster demo,websocket集群,分布式websocket服务

Notifications You must be signed in to change notification settings

yeesuu/go-websocket-cluster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoLang WebSocket Cluster

简介

Golang Websocket 集群方案demo,通过Redis的Pub&Sub功能,实现多个Websocket Server同步消息,解决大并发需求。

依赖

参考文档

参考代码

配置项

  server:
    port: 8000 #运行端口
  redis:
    host: redis
    port: 6379
    password:
    db: 0
  message:
    channel: message_channel #消息channel名称

Docker 单机运行体验

docker compose up -d

使用websocket客户端连接ws://localhost:8000

编译运行

mv config.example.yml config.yml
go build
./go-websocket-cluster

集群部署

部署多套后(多服务器或不同端口),使用SLB或Nginx反向代理实现负载均衡

参见:WebSocket 集群方案总结

示例功能

type 定义请见 entity/entity.go

获取在线人数:

  • request
    {
      "type": 3
    }
  • response
    {
      "type": 1,
      "data": 1,
      "timestamp": 1618811942
    }

获取点赞人数

  • request
    {
      "type": 2
    }
  • response
    {
      "type": 4,
      "data": 0,
      "timestamp": 1618811942
    }

发送普通信息

  • request
    {
      "type": 5,
      "data": "hello"
    }
  • response
    {
      "type": 5,
      "data": "hello",
      "timestamp": 1618811942
    }

更多功能请根据需求自行开发

About

golang websocket cluster demo,websocket集群,分布式websocket服务

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published