71 lines
2.9 KiB
Go
71 lines
2.9 KiB
Go
package websocket
|
|
|
|
import "net/http"
|
|
|
|
// Server is the websocket server,
|
|
// listens on the config's port, the critical part is the event OnConnection
|
|
type Server interface {
|
|
// Set sets an option aka configuration field to the websocket server
|
|
Set(...OptionSetter)
|
|
// Handler returns the http.Handler which is setted to the 'Websocket Endpoint path',
|
|
// the client should target to this handler's developer's custom path
|
|
// ex: http.Handle("/myendpoint", mywebsocket.Handler())
|
|
// Handler calls the HandleConnection, so
|
|
// Use Handler or HandleConnection manually, DO NOT USE both.
|
|
// Note: you can always create your own upgrader which returns an UnderlineConnection and call only the HandleConnection manually (as Iris web framework does)
|
|
Handler() http.Handler
|
|
// HandleConnection creates & starts to listening to a new connection
|
|
// DO NOT USE Handler() and HandleConnection at the sametime, see Handler for more
|
|
// NOTE: You don't need this, this is needed only when we want to 'hijack' the upgrader
|
|
// (used for Iris and fasthttp before Iris v6)
|
|
HandleConnection(*http.Request, UnderlineConnection)
|
|
// OnConnection this is the main event you, as developer, will work with each of the websocket connections
|
|
OnConnection(cb ConnectionFunc)
|
|
|
|
/*
|
|
connection actions, same as the connection's method,
|
|
but these methods accept the connection ID,
|
|
which is useful when the developer maps
|
|
this id with a database field (using config.IDGenerator).
|
|
*/
|
|
|
|
// IsConnected returns true if the connection with that ID is connected to the server
|
|
// useful when you have defined a custom connection id generator (based on a database)
|
|
// and you want to check if that connection is already connected (on multiple tabs)
|
|
IsConnected(connID string) bool
|
|
|
|
// Join joins a websocket client to a room,
|
|
// first parameter is the room name and the second the connection.ID()
|
|
//
|
|
// You can use connection.Join("room name") instead.
|
|
Join(roomName string, connID string)
|
|
|
|
// LeaveAll kicks out a connection from ALL of its joined rooms
|
|
LeaveAll(connID string)
|
|
|
|
// Leave leaves a websocket client from a room,
|
|
// first parameter is the room name and the second the connection.ID()
|
|
//
|
|
// You can use connection.Leave("room name") instead.
|
|
// Returns true if the connection has actually left from the particular room.
|
|
Leave(roomName string, connID string) bool
|
|
|
|
// GetConnectionsByRoom returns a list of Connection
|
|
// are joined to this room.
|
|
GetConnectionsByRoom(roomName string) []Connection
|
|
|
|
// Disconnect force-disconnects a websocket connection
|
|
// based on its connection.ID()
|
|
// What it does?
|
|
// 1. remove the connection from the list
|
|
// 2. leave from all joined rooms
|
|
// 3. fire the disconnect callbacks, if any
|
|
// 4. close the underline connection and return its error, if any.
|
|
//
|
|
// You can use the connection.Disconnect() instead.
|
|
Disconnect(connID string) error
|
|
}
|
|
|
|
type server struct {
|
|
}
|