Socket

Socket represents a WebSocket connection and records your handlers, transport, session and membership.

This API is used in Socket.


Symbols

SymbolClassDescription
WebSocketHandlertypeWebSocketHandler defines a public type of API.
WSEventSignaturetypeWSEventSignature defines a public type of API.
SockettypeSocket defines a public type of API.
Socket.IDfuncReturn the identifier.
Socket.JoinfuncAdd the socket to a group.
Socket.LeavefuncTake the socket back.
Socket.LeaveAllfuncWithdraw LeaveAll.
Socket.GroupsfuncReturns the socket groups.
Socket.EmitTofuncEmite EmitTo.
Socket.IsLocalfuncIndicates the state of IsLocal.
Socket.InstanceIDfuncReturns the identifier of the owner instance.
Socket.DisconnectfuncClose the connection to the indicated code.
Socket.EmitfuncIt emits an event.
Socket.EmitAwaitfuncIt issues a request and decodifies its response.
Socket.WhenfuncSearch a previous handler.
Socket.OnfuncHe records an event handler.
Socket.OnNamedfuncHe records a handler with parameter names.
Socket.OnRegisteredfuncHe registers a handler with his full signature.
Socket.DispatchWhenfuncOffice DispatchWhen.
Socket.DispatchOnfuncOffice DispatchOn.
Socket.OnParameterTypesfuncReturns the types of parameters recorded.
Socket.OnParameterNamesfuncReturns the names of registered parameters.
Socket.OnSignaturefuncReturns the registered signature for an event.

WebSocketHandler

Declaration:

gox
type WebSocketHandler func(socket *Socket)

WebSocketHandler defines a public type of API.


WSEventSignature

Declaration:

gox
type WSEventSignature struct {
	ParameterNames []string
	HasReply       bool
}

WSEventSignature defines a public type of API.

Fields

FieldTypeDescription
ParameterNames[]stringThe name of the handler parameters.
HasReplyboolIt indicates if the handler produces a response.

Socket

Declaration:

gox
type Socket struct {
	Session Session
	Handshake SocketHandshake
	Siblings SocketCollection
}

Socket defines a public type of API.

Fields

FieldTypeDescription
SessionSessionAssociated session.
HandshakeSocketHandshakeHTTP upgrade meta.
SiblingsSocketCollectionSockets from the same session, except for the current socket.

Socket.ID

Declaration:

gox
func (s *Socket) ID() string

Return the identifier.

Returns

ReturnTypeDescription
valuestringTextual value produced by the operation; its zero value is an empty string.

Usage syntax

gox
value := socket.ID()

Socket.Join

Declaration:

gox
func (s *Socket) Join(group string) error

Add the socket to a group.

Parameters

ParameterTypeDescription
groupstringGroup name.

Returns

ReturnTypeDescription
errerrornil 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

gox
err := socket.Join("group")

Socket.Leave

Declaration:

gox
func (s *Socket) Leave(group string) error

Take the socket back.

Parameters

ParameterTypeDescription
groupstringGroup name.

Returns

ReturnTypeDescription
errerrornil 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

gox
err := socket.Leave("group")

Socket.LeaveAll

Declaration:

gox
func (s *Socket) LeaveAll() error

Withdraw LeaveAll.

Returns

ReturnTypeDescription
errerrornil 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

gox
err := socket.LeaveAll()

Socket.Groups

Declaration:

gox
func (s *Socket) Groups() []string

Returns the socket groups.

Returns

ReturnTypeDescription
value[]stringResult collection; can be nil when there are no values.

Usage syntax

gox
value := socket.Groups()

Socket.EmitTo

Declaration:

gox
func (s *Socket) EmitTo(group string, event string, values ...any)

Emite EmitTo.

Parameters

ParameterTypeDescription
groupstringGroup name.
eventstringName of the event.
values...anyValues provided to the operation.

Usage syntax

gox
socket.EmitTo("group", "event", value)

Socket.IsLocal

Declaration:

gox
func (s *Socket) IsLocal() bool

Indicates the state of IsLocal.

Returns

ReturnTypeDescription
valueboolIt indicates the result of the check.

Usage syntax

gox
value := socket.IsLocal()

Socket.InstanceID

Declaration:

gox
func (s *Socket) InstanceID() string

Returns the identifier of the owner instance.

Returns

ReturnTypeDescription
valuestringTextual value produced by the operation; its zero value is an empty string.

Usage syntax

gox
value := socket.InstanceID()

Socket.Disconnect

Declaration:

gox
func (s *Socket) Disconnect(code WSCloseCode)

Close the connection to the indicated code.

Parameters

ParameterTypeDescription
codeWSCloseCodeCode of closure.

Behavior

Use the default ratio associated with the closing code.

Usage syntax

gox
socket.Disconnect(gox.WSCloseNormal)

Socket.Emit

Declaration:

gox
func (s *Socket) Emit(event string, values ...any)

It emits an event.

Parameters

ParameterTypeDescription
eventstringName of the event.
values...anyValues provided to the operation.

Usage syntax

gox
socket.Emit("event", value)

Socket.EmitAwait

Declaration:

gox
func (s *Socket) EmitAwait(out any, event string, values ...any) error

It issues a request and decodifies its response.

Parameters

ParameterTypeDescription
outanyDestination where the result is decoded.
eventstringName of the event.
values...anyValues provided to the operation.

Returns

ReturnTypeDescription
errerrornil 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

gox
err := socket.EmitAwait(&value, "event", value)

Socket.When

Declaration:

gox
func (s *Socket) When(event string, handler any)

Search a previous handler.

Parameters

ParameterTypeDescription
eventstringName of the event.
handleranyType value any required by the operation.

Usage syntax

gox
socket.When("event", nil)

Socket.On

Declaration:

gox
func (s *Socket) On(event string, handler any)

He records an event handler.

Parameters

ParameterTypeDescription
eventstringName of the event.
handleranyType value any required by the operation.

Usage syntax

gox
socket.On("event", nil)

Socket.OnNamed

Declaration:

gox
func (s *Socket) OnNamed(event string, parameterNames []string, handler any)

He records a handler with parameter names.

Parameters

ParameterTypeDescription
eventstringName of the event.
parameterNames[]stringType value []string required by the operation.
handleranyType value any required by the operation.

Usage syntax

gox
socket.OnNamed("event", value, nil)

Socket.OnRegistered

Declaration:

gox
func (s *Socket) OnRegistered(event string, parameterNames []string, hasReply bool, handler any)

He registers a handler with his full signature.

Parameters

ParameterTypeDescription
eventstringName of the event.
parameterNames[]stringType value []string required by the operation.
hasReplyboolType value bool required by the operation.
handleranyType value any required by the operation.

Usage syntax

gox
socket.OnRegistered("event", value, value, nil)

Socket.DispatchWhen

Declaration:

gox
func (s *Socket) DispatchWhen(event string, args ...any) (value any, handled bool, err error)

Office DispatchWhen.

Parameters

ParameterTypeDescription
eventstringName of the event.
args...anyType value ...any required by the operation.

Returns

ReturnTypeDescription
valueanyType value any produced by the operation.
handledboolIt indicates the result of the check.
errerrornil 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:

gox
func (s *Socket) DispatchOn(event string, args ...any) (value any, handled bool, err error)

Office DispatchOn.

Parameters

ParameterTypeDescription
eventstringName of the event.
args...anyType value ...any required by the operation.

Returns

ReturnTypeDescription
valueanyType value any produced by the operation.
handledboolIt indicates the result of the check.
errerrornil 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:

gox
func (s *Socket) OnParameterTypes(event string) ([]reflect.Type, bool, error)

Returns the types of parameters recorded.

Parameters

ParameterTypeDescription
eventstringName of the event.

Returns

ReturnTypeDescription
value[]reflect.TypeResult collection; can be nil when there are no values.
okboolIt indicates the result of the check.
errerrornil 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

gox
value, ok, err := socket.OnParameterTypes("event")

Socket.OnParameterNames

Declaration:

gox
func (s *Socket) OnParameterNames(event string) ([]string, bool)

Returns the names of registered parameters.

Parameters

ParameterTypeDescription
eventstringName of the event.

Returns

ReturnTypeDescription
value[]stringResult collection; can be nil when there are no values.
okboolIt indicates the result of the check.

Usage syntax

gox
value, ok := socket.OnParameterNames("event")

Socket.OnSignature

Declaration:

gox
func (s *Socket) OnSignature(event string) (WSEventSignature, bool)

Returns the registered signature for an event.

Parameters

ParameterTypeDescription
eventstringName of the event.

Returns

ReturnTypeDescription
valueWSEventSignatureType value WSEventSignature produced by the operation.
okboolIt indicates the result of the check.

Usage syntax

gox
value, ok := socket.OnSignature("event")

See also

Next step

Continue with handshake metadata.

SocketHandshake