增加对当前连接发送消息的方法

This commit is contained in:
白茶清欢 2021-03-28 17:21:47 +08:00
parent c41a7c4671
commit fef46f3a0b
2 changed files with 27 additions and 0 deletions

View File

@ -10,6 +10,8 @@ package main
import (
"fmt"
"github.com/go-developer/websocket/message"
"github.com/go-developer/websocket"
"github.com/go-developer/websocket/abstract"
"github.com/go-developer/websocket/context"
@ -24,6 +26,7 @@ type Example struct {
func (e Example) Connect(ctx *context.WSContext) {
fmt.Println("建立连接成功")
message.Response(ctx, map[string]interface{}{"say": "hello world!"})
}
func (e Example) Disconnect(ctx *context.WSContext) {

24
message/message.go Normal file
View File

@ -0,0 +1,24 @@
// Package message ...
//
// Description : 消息相关操作
//
// Author : go_developer@163.com<张德满>
//
// Date : 2021-03-28 5:15 下午
package message
import (
"encoding/json"
"github.com/go-developer/websocket/context"
)
// Response 对当前连接的相关行为做出响应
//
// Author : go_developer@163.com<张德满>
//
// 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)
}