2021-03-28 17:21:47 +08:00
|
|
|
// Package message ...
|
|
|
|
//
|
|
|
|
// Description : 消息相关操作
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-28 17:21:47 +08:00
|
|
|
//
|
|
|
|
// Date : 2021-03-28 5:15 下午
|
|
|
|
package message
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2021-04-08 18:36:17 +08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2021-11-17 18:30:49 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/websocket/context"
|
2021-03-28 17:21:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Response 对当前连接的相关行为做出响应
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-28 17:21:47 +08:00
|
|
|
//
|
|
|
|
// Date : 5:17 下午 2021/3/28
|
|
|
|
func Response(wsCtx *context.WSContext, data map[string]interface{}) error {
|
|
|
|
byteData, _ := json.Marshal(data)
|
|
|
|
return wsCtx.Session.Write(byteData)
|
|
|
|
}
|
2021-04-08 18:36:17 +08:00
|
|
|
|
|
|
|
// Broadcast 消息广播
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-04-08 18:36:17 +08:00
|
|
|
//
|
|
|
|
// Date : 5:43 下午 2021/4/8
|
|
|
|
func Broadcast(message string, ctxList ...context.WSContext) map[string]error {
|
|
|
|
result := make(map[string]error)
|
|
|
|
for _, ctx := range ctxList {
|
|
|
|
if nil == ctx.Session {
|
|
|
|
result[ctx.ConnectionID] = errors.New("session is nil")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result[ctx.ConnectionID] = ctx.Session.Write([]byte(message))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|