99774ac513
Author: 白茶清欢 <go_developer@163.com>
86 lines
1.9 KiB
Go
86 lines
1.9 KiB
Go
// Package main ...
|
|
//
|
|
// Description : 启动一个ws server 的示例
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2021-03-28 3:57 下午
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/logger"
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/websocket/config"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/websocket/message"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/websocket"
|
|
"git.zhangdeman.cn/zhangdeman/websocket/abstract"
|
|
"git.zhangdeman.cn/zhangdeman/websocket/context"
|
|
)
|
|
|
|
func main() {
|
|
_ = websocket.NewWebsocketServe(nil, &Example{})
|
|
}
|
|
|
|
type Example struct {
|
|
}
|
|
|
|
func (e Example) Connect(ctx *context.WSContext) error {
|
|
fmt.Println("建立连接成功")
|
|
_ = message.Response(ctx, map[string]interface{}{"say": "hello world!", "cid": ctx.ConnectionID})
|
|
return nil
|
|
}
|
|
|
|
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 {
|
|
return []abstract.ICommand{&exampleCommand{}}
|
|
}
|
|
|
|
func (e Example) GetModuleFlag() string {
|
|
return "example"
|
|
}
|
|
|
|
func (e Example) GetServerPort() int {
|
|
return 10099
|
|
}
|
|
|
|
func (e Example) GetWSServerConfig() []config.SetWSServerConfig {
|
|
return []config.SetWSServerConfig{
|
|
config.SetWSServerLogEnable("./logs", e.GetModuleFlag()+".log", zapcore.DebugLevel, logger.TimeIntervalTypeHour),
|
|
config.EnablePprof(10100),
|
|
}
|
|
}
|
|
|
|
type exampleCommand struct {
|
|
}
|
|
|
|
func (e exampleCommand) GetCommand() string {
|
|
return "ping"
|
|
}
|
|
|
|
func (e exampleCommand) GetConfigOption() []config.SetCommandConfig {
|
|
return []config.SetCommandConfig{config.ClosePushCommandErrorMessage()}
|
|
}
|
|
|
|
func (e exampleCommand) Execute(ctx *context.WSContext, data []byte) (interface{}, error) {
|
|
return map[string]interface{}{"ping": "pong"}, nil
|
|
}
|