Socket
Socket represents a WebSocket connection and records your handlers, transport, session and membership.
This API is used in Socket.
Symbols
| Symbol | Class | Description |
|---|---|---|
| WebSocketHandler | type | WebSocketHandler defines a public type of API. |
| WSEventSignature | type | WSEventSignature defines a public type of API. |
| Socket | type | Socket defines a public type of API. |
| Socket.ID | func | Return the identifier. |
| Socket.Join | func | Add the socket to a group. |
| Socket.Leave | func | Take the socket back. |
| Socket.LeaveAll | func | Withdraw LeaveAll. |
| Socket.Groups | func | Returns the socket groups. |
| Socket.EmitTo | func | Emite EmitTo. |
| Socket.IsLocal | func | Indicates the state of IsLocal. |
| Socket.InstanceID | func | Returns the identifier of the owner instance. |
| Socket.Disconnect | func | Close the connection to the indicated code. |
| Socket.Emit | func | It emits an event. |
| Socket.EmitAwait | func | It issues a request and decodifies its response. |
| Socket.When | func | Search a previous handler. |
| Socket.On | func | He records an event handler. |
| Socket.OnNamed | func | He records a handler with parameter names. |
| Socket.OnRegistered | func | He registers a handler with his full signature. |
| Socket.DispatchWhen | func | Office DispatchWhen. |
| Socket.DispatchOn | func | Office DispatchOn. |
| Socket.OnParameterTypes | func | Returns the types of parameters recorded. |
| Socket.OnParameterNames | func | Returns the names of registered parameters. |
| Socket.OnSignature | func | Returns the registered signature for an event. |
WebSocketHandler
Declaration:
type WebSocketHandler func(socket *Socket) WebSocketHandler defines a public type of API.
WSEventSignature
Declaration:
type WSEventSignature struct {
ParameterNames []string
HasReply bool
} WSEventSignature defines a public type of API.
Fields
| Field | Type | Description |
|---|---|---|
ParameterNames | []string | The name of the handler parameters. |
HasReply | bool | It indicates if the handler produces a response. |
Socket
Declaration:
type Socket struct {
Session Session
Handshake SocketHandshake
Siblings SocketCollection
} Socket defines a public type of API.
Fields
| Field | Type | Description |
|---|---|---|
Session | Session | Associated session. |
Handshake | SocketHandshake | HTTP upgrade meta. |
Siblings | SocketCollection | Sockets from the same session, except for the current socket. |
Socket.ID
Declaration:
func (s *Socket) ID() string Return the identifier.
Returns
| Return | Type | Description |
|---|---|---|
value | string | Textual value produced by the operation; its zero value is an empty string. |
Usage syntax
value := socket.ID() Socket.Join
Declaration:
func (s *Socket) Join(group string) error Add the socket to a group.
Parameters
| Parameter | Type | Description |
|---|---|---|
group | string | Group name. |
Returns
| Return | Type | Description |
|---|---|---|
err | error | nil if the operation ends correctly; otherwise, the error produced. |
Behavior
The operation returns an error when it cannot complete the above contract.
Errors
Join returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.
Usage syntax
err := socket.Join("group") Socket.Leave
Declaration:
func (s *Socket) Leave(group string) error Take the socket back.
Parameters
| Parameter | Type | Description |
|---|---|---|
group | string | Group name. |
Returns
| Return | Type | Description |
|---|---|---|
err | error | nil if the operation ends correctly; otherwise, the error produced. |
Behavior
The operation returns an error when it cannot complete the above contract.
Errors
Leave returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.
Usage syntax
err := socket.Leave("group") Socket.LeaveAll
Declaration:
func (s *Socket) LeaveAll() error Withdraw LeaveAll.
Returns
| Return | Type | Description |
|---|---|---|
err | error | nil if the operation ends correctly; otherwise, the error produced. |
Behavior
The operation returns an error when it cannot complete the above contract.
Errors
LeaveAll returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.
Usage syntax
err := socket.LeaveAll() Socket.Groups
Declaration:
func (s *Socket) Groups() []string Returns the socket groups.
Returns
| Return | Type | Description |
|---|---|---|
value | []string | Result collection; can be nil when there are no values. |
Usage syntax
value := socket.Groups() Socket.EmitTo
Declaration:
func (s *Socket) EmitTo(group string, event string, values ...any) Emite EmitTo.
Parameters
| Parameter | Type | Description |
|---|---|---|
group | string | Group name. |
event | string | Name of the event. |
values | ...any | Values provided to the operation. |
Usage syntax
socket.EmitTo("group", "event", value) Socket.IsLocal
Declaration:
func (s *Socket) IsLocal() bool Indicates the state of IsLocal.
Returns
| Return | Type | Description |
|---|---|---|
value | bool | It indicates the result of the check. |
Usage syntax
value := socket.IsLocal() Socket.InstanceID
Declaration:
func (s *Socket) InstanceID() string Returns the identifier of the owner instance.
Returns
| Return | Type | Description |
|---|---|---|
value | string | Textual value produced by the operation; its zero value is an empty string. |
Usage syntax
value := socket.InstanceID() Socket.Disconnect
Declaration:
func (s *Socket) Disconnect(code WSCloseCode) Close the connection to the indicated code.
Parameters
| Parameter | Type | Description |
|---|---|---|
code | WSCloseCode | Code of closure. |
Behavior
Use the default ratio associated with the closing code.
Usage syntax
socket.Disconnect(gox.WSCloseNormal) Socket.Emit
Declaration:
func (s *Socket) Emit(event string, values ...any) It emits an event.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Name of the event. |
values | ...any | Values provided to the operation. |
Usage syntax
socket.Emit("event", value) Socket.EmitAwait
Declaration:
func (s *Socket) EmitAwait(out any, event string, values ...any) error It issues a request and decodifies its response.
Parameters
| Parameter | Type | Description |
|---|---|---|
out | any | Destination where the result is decoded. |
event | string | Name of the event. |
values | ...any | Values provided to the operation. |
Returns
| Return | Type | Description |
|---|---|---|
err | error | nil if the operation ends correctly; otherwise, the error produced. |
Behavior
It blocks until you receive a response or until the transport makes a mistake.
Errors
EmitAwait returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.
Usage syntax
err := socket.EmitAwait(&value, "event", value) Socket.When
Declaration:
func (s *Socket) When(event string, handler any) Search a previous handler.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Name of the event. |
handler | any | Type value any required by the operation. |
Usage syntax
socket.When("event", nil) Socket.On
Declaration:
func (s *Socket) On(event string, handler any) He records an event handler.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Name of the event. |
handler | any | Type value any required by the operation. |
Usage syntax
socket.On("event", nil) Socket.OnNamed
Declaration:
func (s *Socket) OnNamed(event string, parameterNames []string, handler any) He records a handler with parameter names.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Name of the event. |
parameterNames | []string | Type value []string required by the operation. |
handler | any | Type value any required by the operation. |
Usage syntax
socket.OnNamed("event", value, nil) Socket.OnRegistered
Declaration:
func (s *Socket) OnRegistered(event string, parameterNames []string, hasReply bool, handler any) He registers a handler with his full signature.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Name of the event. |
parameterNames | []string | Type value []string required by the operation. |
hasReply | bool | Type value bool required by the operation. |
handler | any | Type value any required by the operation. |
Usage syntax
socket.OnRegistered("event", value, value, nil) Socket.DispatchWhen
Declaration:
func (s *Socket) DispatchWhen(event string, args ...any) (value any, handled bool, err error) Office DispatchWhen.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Name of the event. |
args | ...any | Type value ...any required by the operation. |
Returns
| Return | Type | Description |
|---|---|---|
value | any | Type value any produced by the operation. |
handled | bool | It indicates the result of the check. |
err | error | nil if the operation ends correctly; otherwise, the error produced. |
Behavior
The operation returns an error when it cannot complete the above contract.
Errors
DispatchWhen returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.
Socket.DispatchOn
Declaration:
func (s *Socket) DispatchOn(event string, args ...any) (value any, handled bool, err error) Office DispatchOn.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Name of the event. |
args | ...any | Type value ...any required by the operation. |
Returns
| Return | Type | Description |
|---|---|---|
value | any | Type value any produced by the operation. |
handled | bool | It indicates the result of the check. |
err | error | nil if the operation ends correctly; otherwise, the error produced. |
Behavior
The operation returns an error when it cannot complete the above contract.
Errors
DispatchOn returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.
Socket.OnParameterTypes
Declaration:
func (s *Socket) OnParameterTypes(event string) ([]reflect.Type, bool, error) Returns the types of parameters recorded.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Name of the event. |
Returns
| Return | Type | Description |
|---|---|---|
value | []reflect.Type | Result collection; can be nil when there are no values. |
ok | bool | It indicates the result of the check. |
err | error | nil if the operation ends correctly; otherwise, the error produced. |
Behavior
The operation returns an error when it cannot complete the above contract.
Errors
OnParameterTypes returns an error when the arguments, the status of the receiver or the underlying operation make it impossible to complete the call.
Usage syntax
value, ok, err := socket.OnParameterTypes("event") Socket.OnParameterNames
Declaration:
func (s *Socket) OnParameterNames(event string) ([]string, bool) Returns the names of registered parameters.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Name of the event. |
Returns
| Return | Type | Description |
|---|---|---|
value | []string | Result collection; can be nil when there are no values. |
ok | bool | It indicates the result of the check. |
Usage syntax
value, ok := socket.OnParameterNames("event") Socket.OnSignature
Declaration:
func (s *Socket) OnSignature(event string) (WSEventSignature, bool) Returns the registered signature for an event.
Parameters
| Parameter | Type | Description |
|---|---|---|
event | string | Name of the event. |
Returns
| Return | Type | Description |
|---|---|---|
value | WSEventSignature | Type value WSEventSignature produced by the operation. |
ok | bool | It indicates the result of the check. |
Usage syntax
value, ok := socket.OnSignature("event")