Skip to content

Latest commit

 

History

History
50 lines (38 loc) · 989 Bytes

README.md

File metadata and controls

50 lines (38 loc) · 989 Bytes

使用方式

说明

  • 基于github.com/gorilla/websocket
  • 增加了client_id,区分客户端
  • 可以发送消息到指定client_id

导入包

go get github.com/lixiang4u/go-websocket

实例化对象

var ws = go_websocket.WSWrapper{}

注册响应事件

ws.On(eventName string, f eventHandler)
  • 需要客户端请求数据格式为protocol对象的json字面量
type protocol struct {
	Event    string      `json:"event"`
	Data     interface{} `json:"data"`
}
  • 测试返回的数据格式为protocol对象的json字面量
type protocol struct {
	ClientId string      `json:"client_id"`
	Event    string      `json:"event"`
	Data     interface{} `json:"data"`
}

运行

ws.Run(w http.ResponseWriter, r *http.Request, responseHeader http.Header)

广播聊天截图

markdown