Documentation
¶
Index ¶
- func Extract(hostPort string, lis net.Listener) (string, error)
- func Port(lis net.Listener) (int, bool)
- type BasePlayer
- type MethodDesc
- type Server
- func (s *Server) Dispatch(sessionCtx SessionContext, msg *protocol.RequestPacket) (*protocol.ResponsePacket, error)
- func (s *Server) Endpoint() (*url.URL, error)
- func (s *Server) RegisterService(sd *ServiceDesc, ss any)
- func (s *Server) Start(ctx context.Context) error
- func (s *Server) Stop(ctx context.Context) error
- type ServerOption
- type ServiceDesc
- type ServiceDispatcher
- type ServiceRegistrar
- type Session
- type SessionContext
- type SessionManager
- func (sm *SessionManager) Broadcast(message []byte)
- func (sm *SessionManager) CloseAll()
- func (sm *SessionManager) GetSession(id string) *Session
- func (sm *SessionManager) GetSessionByPlayerID(playerID int64) *Session
- func (sm *SessionManager) GetSessionCount() int
- func (sm *SessionManager) RegisterSession(session *Session)
- func (sm *SessionManager) Start()
- func (sm *SessionManager) UnregisterSession(session *Session)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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) 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.
type ServerOption ¶
type ServerOption func(*Server)
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 创建新的会话
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 (*SessionManager) Broadcast ¶
func (sm *SessionManager) Broadcast(message []byte)
Broadcast 广播消息给所有会话
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) UnregisterSession ¶
func (sm *SessionManager) UnregisterSession(session *Session)
UnregisterSession 注销会话