websocket

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2025 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Extract added in v0.0.16

func Extract(hostPort string, lis net.Listener) (string, error)

Extract returns a private addr and port.

func Port added in v0.0.16

func Port(lis net.Listener) (int, bool)

Port return a real port.

Types

type BasePlayer

type BasePlayer interface {
	GetPlayerID() int64
}

type MethodDesc

type MethodDesc struct {
	MethodName string
	MessageID  uint32
	Handler    methodHandler
	// contains filtered or unexported fields
}

MethodDesc represents an RPC service's method specification.

type Server

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

func NewServer

func NewServer(opts ...ServerOption) *Server

func (*Server) Dispatch

func (s *Server) Dispatch(sessionCtx SessionContext, msg *protocol.RequestPacket) (*protocol.ResponsePacket, error)

func (*Server) Endpoint added in v0.0.15

func (s *Server) Endpoint() (*url.URL, error)

func (*Server) RegisterService

func (s *Server) RegisterService(sd *ServiceDesc, ss any)

RegisterService registers a service and its implementation to the gRPC server. It is called from the IDL generated code. This must be called before invoking Serve. If ss is non-nil (for legacy code), its type is checked to ensure it implements sd.HandlerType.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

type ServerOption

type ServerOption func(*Server)

func Address

func Address(addr string) ServerOption

Address with server address.

func Network

func Network(network string) ServerOption

Network with server network.

func Timeout

func Timeout(timeout time.Duration) ServerOption

type ServiceDesc

type ServiceDesc struct {
	ServiceName string
	HandlerType any
	Methods     []MethodDesc
	Metadata    any
}

ServiceDesc represents an RPC service's specification.

type ServiceDispatcher

type ServiceDispatcher interface {
	Dispatch(sessionCtx SessionContext, msg *protocol.RequestPacket) (*protocol.ResponsePacket, error)
}

type ServiceRegistrar

type ServiceRegistrar interface {
	// RegisterService registers a service and its implementation to the
	// concrete type implementing this interface.  It may not be called
	// once the server has started serving.
	// desc describes the service and its methods and handlers. impl is the
	// service implementation which is passed to the method handlers.
	RegisterService(desc *ServiceDesc, impl any)
}

ServiceRegistrar wraps a single method that supports service registration. It enables users to pass concrete types other than grpc.Server to the service registration methods exported by the IDL generated code.

type Session

type Session struct {
	ID string
	// contains filtered or unexported fields
}

Session 表示一个 WebSocket 会话

func NewSession

func NewSession(id string, conn *websocket.Conn, manager *SessionManager, disp ServiceDispatcher) *Session

NewSession 创建新的会话

func (*Session) Close

func (s *Session) Close()

Close 关闭会话

func (*Session) ReadPump

func (s *Session) ReadPump()

ReadPump 处理来自客户端的消息读取

func (*Session) Send

func (s *Session) Send(message []byte) bool

Send 发送消息到会话

func (*Session) WritePump

func (s *Session) WritePump()

WritePump 处理向客户端发送消息

type SessionContext

type SessionContext interface {
	// 只读数据
	Context() context.Context

	GetPlayer() BasePlayer

	// 发送消息
	Send(msgID uint32, data proto.Message) bool

	// 发送原始数据
	SendRaw(msgID uint32, data []byte) bool

	// 给指定玩家发送数据
	SendByPlayer(playerID int64, msgID uint32, data proto.Message) (bool, error)

	// 给指定玩家发送原始数据
	SendRawByPlayer(playerID int64, msgID uint32, data []byte) (bool, error)

	// 绑定session后关联的玩家信息
	SetPlayer(player BasePlayer)

	WithContext(ctx context.Context) SessionContext
}

func NewSessionContext

func NewSessionContext(session *Session) SessionContext

type SessionManager

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

SessionManager 管理所有活跃的会话

func NewSessionManager

func NewSessionManager() *SessionManager

NewSessionManager 创建新的会话管理器

func (*SessionManager) Broadcast

func (sm *SessionManager) Broadcast(message []byte)

Broadcast 广播消息给所有会话

func (*SessionManager) CloseAll

func (sm *SessionManager) CloseAll()

CloseAll 关闭所有会话

func (*SessionManager) GetSession

func (sm *SessionManager) GetSession(id string) *Session

GetSession 获取指定ID的会话

func (*SessionManager) GetSessionByPlayerID

func (sm *SessionManager) GetSessionByPlayerID(playerID int64) *Session

func (*SessionManager) GetSessionCount

func (sm *SessionManager) GetSessionCount() int

GetSessionCount 获取当前会话数量

func (*SessionManager) RegisterSession

func (sm *SessionManager) RegisterSession(session *Session)

RegisterSession 注册新会话

func (*SessionManager) Start

func (sm *SessionManager) Start()

Start 启动会话管理器

func (*SessionManager) UnregisterSession

func (sm *SessionManager) UnregisterSession(session *Session)

UnregisterSession 注销会话

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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