2021-03-28 16:04:42 +08:00
|
|
|
// Package main ...
|
|
|
|
//
|
|
|
|
// Description : 启动一个ws server 的示例
|
|
|
|
//
|
2021-06-05 22:13:09 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-28 16:04:42 +08:00
|
|
|
//
|
|
|
|
// Date : 2021-03-28 3:57 下午
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-11-17 18:30:49 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/websocket/config"
|
2021-04-17 15:04:43 +08:00
|
|
|
|
2021-11-17 18:30:49 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/websocket/message"
|
2021-03-28 17:21:47 +08:00
|
|
|
|
2021-11-17 18:30:49 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/websocket"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/websocket/abstract"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/websocket/context"
|
2021-03-28 16:04:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-11-17 18:54:34 +08:00
|
|
|
_ = websocket.NewWebsocketServe(nil, &Example{})
|
2021-03-28 16:04:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Example struct {
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:13:22 +08:00
|
|
|
func (e Example) Connect(ctx *context.WSContext) error {
|
2021-03-28 16:04:42 +08:00
|
|
|
fmt.Println("建立连接成功")
|
2021-11-17 18:54:34 +08:00
|
|
|
_ = message.Response(ctx, map[string]interface{}{"say": "hello world!", "cid": ctx.ConnectionID})
|
2021-04-09 15:13:22 +08:00
|
|
|
return nil
|
2021-03-28 16:04:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e Example) Disconnect(ctx *context.WSContext) {
|
|
|
|
fmt.Println("断开连接成功")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Example) Close(ctx *context.WSContext, code int, message string) error {
|
|
|
|
fmt.Println("关闭连接成功")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Example) HandshakeURL() []string {
|
|
|
|
return []string{
|
|
|
|
"/ws/test",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Example) GetCommandList() []abstract.ICommand {
|
2021-04-09 16:08:00 +08:00
|
|
|
return []abstract.ICommand{&exampleCommand{}}
|
2021-03-28 16:04:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e Example) GetModuleFlag() string {
|
|
|
|
return "example"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e Example) GetServerPort() int {
|
|
|
|
return 10099
|
|
|
|
}
|
2021-04-09 16:08:00 +08:00
|
|
|
|
2021-04-17 21:41:40 +08:00
|
|
|
func (e Example) GetWSServerConfig() []config.SetWSServerConfig {
|
|
|
|
return []config.SetWSServerConfig{
|
2024-07-22 16:40:33 +08:00
|
|
|
// config.SetWSServerLogEnable("./logs", e.GetModuleFlag()+".log", zapcore.DebugLevel, consts.LogSplitHour),
|
|
|
|
// config.EnablePprof(10100),
|
2021-04-17 21:41:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 16:08:00 +08:00
|
|
|
type exampleCommand struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e exampleCommand) GetCommand() string {
|
|
|
|
return "ping"
|
|
|
|
}
|
|
|
|
|
2021-04-17 15:04:43 +08:00
|
|
|
func (e exampleCommand) GetConfigOption() []config.SetCommandConfig {
|
|
|
|
return []config.SetCommandConfig{config.ClosePushCommandErrorMessage()}
|
|
|
|
}
|
|
|
|
|
2021-04-18 21:55:41 +08:00
|
|
|
func (e exampleCommand) Execute(ctx *context.WSContext, data []byte) (interface{}, error) {
|
|
|
|
return map[string]interface{}{"ping": "pong"}, nil
|
2021-04-09 16:08:00 +08:00
|
|
|
}
|