Skip to content

lixiang4u/go-websocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

使用方式

说明

  • 基于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

Releases

No releases published

Packages

No packages published