zzserver

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2022 License: MulanPSL-2.0 Imports: 18 Imported by: 0

README

zzServer

该项目是一个简易的socket服务端程序,支持webSocket以及普通tcpSocket


//开启服务
srv := zzserver.NewZZServer()
srv.SetRouter(&P{}) //绑定路由接口
srv.WsPort = 9999   //websocket端口
//srv.TcpPort = 9988  //不设置就不会启动监听
srv.Start()

新增支持text消息和binary消息 通过?messageType=binary 或者 ?messageType=text 来设置messageType设置 TextMessage,BinaryMessage

ws://127.0.0.1:9999?messageType=binary   发送binary消息

ws://127.0.0.1:9999?messageType=text  发送字符串消息 (默认值)

请参考 example.go

by. douya

有疑问请联系QQ:330496906

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseRouter

type BaseRouter struct{}

BaseRouter 用于重写, 这样就不需要写出所有方法

func (*BaseRouter) OnConnected

func (b *BaseRouter) OnConnected(c *Client)

func (*BaseRouter) OnDisconnect

func (b *BaseRouter) OnDisconnect(c *Client)

func (*BaseRouter) OnMessage

func (b *BaseRouter) OnMessage(c *Client, message []byte)

func (*BaseRouter) OnServerClose

func (b *BaseRouter) OnServerClose()

func (*BaseRouter) OnServerUpdate

func (b *BaseRouter) OnServerUpdate()

type Client

type Client struct {
	Server          *Server     //server对线
	User            interface{} //用户对象
	UserData        sync.Map    //保存一些用户自定义内容
	ConnectionIndex int         //
	LastMsgTime     time.Time   //最后收到消息时间
	Ip              string      //登录IP
	WsMessageType   int         //websocket的协议类型,1:text, 2:binary
	// contains filtered or unexported fields
}

Client 保存客户端连接和用户

func (*Client) Close

func (c *Client) Close()

func (*Client) GetRemoteAddr

func (c *Client) GetRemoteAddr() string

GetRemoteAddr 获取地址

func (*Client) SendByte

func (c *Client) SendByte(bytes []byte)

SendByte 发送byte, socket会进行编码后再发送

func (*Client) SendJson

func (c *Client) SendJson(obj interface{}) error

SendJson 先把obj转为json对象再发送

func (*Client) SendText

func (c *Client) SendText(msg string)

SendText 发送消息 封装

type IRouter

type IRouter interface {
	OnMessage(c *Client, message []byte) // OnMessage 用户协程中调用
	OnDisconnect(c *Client)              // OnDisconnect hub协成中调用
	OnServerClose()                      // 关闭服务器执行一次
	OnServerUpdate()
	OnConnected(c *Client)
}

IRouter 方法2: 接口

type Server

type Server struct {

	//监听的端口
	WsPort  int
	TcpPort int

	ConnectionIndex int64 //连接计数
	// contains filtered or unexported fields
}

Server maintains the set of active clients and broadcasts messages to the clients.

func NewZZServer

func NewZZServer() *Server

func (*Server) Close

func (h *Server) Close()

func (*Server) Online

func (h *Server) Online() int

func (*Server) Range

func (h *Server) Range(f func(c *Client) bool)

Range 在同一个go中执行, 不要执行很久的操作

func (*Server) SendToAll

func (h *Server) SendToAll(message []byte)

SendToAll 广播消息

func (*Server) SendToAllJson

func (h *Server) SendToAllJson(obj interface{})

SendToAllJson 广播消息

func (*Server) SetRouter

func (h *Server) SetRouter(r IRouter)

func (*Server) Start

func (h *Server) Start()

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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