增加对当前连接发送消息的方法
This commit is contained in:
parent
c41a7c4671
commit
fef46f3a0b
@ -10,6 +10,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/go-developer/websocket/message"
|
||||||
|
|
||||||
"github.com/go-developer/websocket"
|
"github.com/go-developer/websocket"
|
||||||
"github.com/go-developer/websocket/abstract"
|
"github.com/go-developer/websocket/abstract"
|
||||||
"github.com/go-developer/websocket/context"
|
"github.com/go-developer/websocket/context"
|
||||||
@ -24,6 +26,7 @@ type Example struct {
|
|||||||
|
|
||||||
func (e Example) Connect(ctx *context.WSContext) {
|
func (e Example) Connect(ctx *context.WSContext) {
|
||||||
fmt.Println("建立连接成功")
|
fmt.Println("建立连接成功")
|
||||||
|
message.Response(ctx, map[string]interface{}{"say": "hello world!"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Example) Disconnect(ctx *context.WSContext) {
|
func (e Example) Disconnect(ctx *context.WSContext) {
|
||||||
|
24
message/message.go
Normal file
24
message/message.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user