长链接
Go to file
2021-06-05 22:13:09 +08:00
abstract 调整注释 2021-06-05 22:13:09 +08:00
config 调整注释 2021-06-05 22:13:09 +08:00
context 调整注释 2021-06-05 22:13:09 +08:00
example 调整注释 2021-06-05 22:13:09 +08:00
message 调整注释 2021-06-05 22:13:09 +08:00
storage 调整注释 2021-06-05 22:13:09 +08:00
.gitignore server支持自定义配置 2021-04-17 21:41:40 +08:00
construct.go 调整注释 2021-06-05 22:13:09 +08:00
define.go 调整注释 2021-06-05 22:13:09 +08:00
go.mod update go mod 2021-04-19 12:07:28 +08:00
go.sum update go mod 2021-04-19 12:07:28 +08:00
LICENSE Initial commit 2021-03-27 18:22:23 +08:00
logo.jpg update readme 2021-04-19 23:20:00 +08:00
README.md update readme 2021-04-19 23:20:00 +08:00

介绍

作用

基础的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"

    "github.com/go-developer/websocket/message"

    "github.com/go-developer/websocket"
    "github.com/go-developer/websocket/abstract"
    "github.com/go-developer/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\}

完成

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