websocket

package module
v0.0.0-...-c5ea194 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

README

介绍

作用

基础的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\}

完成

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

Documentation

Overview

Package websocket...

Description : websocket...

Author : go_developer@163.com<白茶清欢>

Date : 2021-03-27 6:49 下午

Package websocket ...

Description : 数据结构定义

Author : go_developer@163.com<白茶清欢>

Date : 2021-04-18 7:53 下午

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewWebsocketServe

func NewWebsocketServe(finishHook func(), wsInstanceList ...abstract.IWebsocket) error

NewWebsocketServe 启动websocket服务

Author : go_developer@163.com<白茶清欢>

Date : 6:49 下午 2021/3/27

func NewWebsocketServeWithGinRouter

func NewWebsocketServeWithGinRouter(ginPort int, ginRouter *gin.Engine, wsInstanceList ...abstract.IWebsocket) error

NewWebsocketServeWithGinRouter ...

Author : go_developer@163.com<白茶清欢>

Date : 17:41 2024/7/22

func Stop

func Stop()

Stop 停止服务

Author : go_developer@163.com<白茶清欢>

Date : 3:55 下午 2021/3/28

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server ...

Author : go_developer@163.com<白茶清欢>

Date : 8:04 下午 2021/3/27

func GetWsServer

func GetWsServer(port int, serverFlag string) (*Server, error)

GetWsServer 获取ws Server

Author : go_developer@163.com<白茶清欢>

Date : 18:36 2024/7/22

func (*Server) GetConfig

func (s *Server) GetConfig() *config.WSServerConfig

GetConfig 获取service实例配置

Author : go_developer@163.com<白茶清欢>

Date : 18:36 2024/7/22

func (*Server) GetMelody

func (s *Server) GetMelody() *melody.Melody

GetMelody 获取websocket实例

Author : go_developer@163.com<白茶清欢>

Date : 18:37 2024/7/22

Directories

Path Synopsis
Package abstract...
Package abstract...
Package config ...
Package config ...
Package context...
Package context...
Package main ...
Package main ...
Package message ...
Package message ...
Package storage ...
Package storage ...

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL