长链接
Go to file
白茶清欢 85feada512 修复cid生成异常问题 2024-07-23 15:52:10 +08:00
abstract 更新github.com导入 => git.zhangdeman.cn 2021-11-17 18:30:49 +08:00
config 升级依赖库 2024-07-22 16:40:33 +08:00
context 修复cid生成异常问题 2024-07-23 15:52:10 +08:00
example 升级依赖库 2024-07-22 16:40:33 +08:00
message 更新github.com导入 => git.zhangdeman.cn 2021-11-17 18:30:49 +08:00
storage 升级依赖库 2024-07-22 16:40:33 +08:00
.gitignore server支持自定义配置 2021-04-17 21:41:40 +08:00
LICENSE Initial commit 2021-03-27 18:22:23 +08:00
README.md 更新github.com导入 => git.zhangdeman.cn 2021-11-17 18:30:49 +08:00
construct.go 增加获取长连接引擎 + 获取server配置方法 2024-07-22 18:38:48 +08:00
define.go 升级依赖库 2024-07-22 16:40:33 +08:00
go.mod update go mod 2024-07-22 22:00:12 +08:00
go.sum 升级依赖库 2024-07-22 16:40:33 +08:00
logo.jpg update readme 2021-04-19 23:20:00 +08:00

README.md

介绍

作用

基础的websocket长连接框架,屏蔽ws基础细节,只关注业务开发即可

运行项目

方案一

git clone git@github.com:go-developer/websocket.
cd websocket/example
go run server.go

方案二

  • 创建项目 : mkdir my_webxocket && cd my_websocket && go mod init my_websocket
  • 创建server.go, 并将下面的代码放入server.go
  • go run server.go
package main

import (
    "fmt"

    "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(&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
}

测试

推荐一个长连接在线测试网站 : Websocket长连接在线测试

连接地址框内输入 : ws://localhost:10099/example/ws/test

点击连接, 会收到类似 {\cid\:\19216806-20210416220530-bb563e745de89e595e02bb7b791677b0\,\say\:\hello world!\} 服务端响应

在输入框内输入 {"command":"ping"} , 会收到服务端响应 : {\ping\:\pong\}

完成

至此一个基本的场链接服务已经成功搭建,并开始运行