2019-06-02 16:49:45 +02:00
package websocket
2017-07-10 17:32:42 +02:00
2019-06-02 16:49:45 +02:00
import (
2019-08-03 03:41:09 +02:00
"net/http"
2019-10-25 00:27:02 +02:00
"github.com/kataras/iris/v12/context"
2019-06-02 16:49:45 +02:00
"github.com/kataras/neffos"
"github.com/kataras/neffos/gobwas"
"github.com/kataras/neffos/gorilla"
2019-07-14 19:22:59 +02:00
"github.com/kataras/neffos/stackexchange/nats"
2019-07-11 11:59:11 +02:00
"github.com/kataras/neffos/stackexchange/redis"
2019-06-02 16:49:45 +02:00
)
var (
2019-07-11 11:59:11 +02:00
// EnableDebug enables debug mode for websocket module,
// for MVC this is done automatically
// when the app's logger level is set to "debug".
EnableDebug = neffos . EnableDebug
2019-06-02 16:49:45 +02:00
// GorillaUpgrader is an upgrader type for the gorilla/websocket subprotocol implementation.
// Should be used on `New` to construct the websocket server.
GorillaUpgrader = gorilla . Upgrader
// GobwasUpgrader is an upgrader type for the gobwas/ws subprotocol implementation.
// Should be used on `New` to construct the websocket server.
GobwasUpgrader = gobwas . Upgrader
// DefaultGorillaUpgrader is a gorilla/websocket Upgrader with all fields set to the default values.
DefaultGorillaUpgrader = gorilla . DefaultUpgrader
// DefaultGobwasUpgrader is a gobwas/ws Upgrader with all fields set to the default values.
DefaultGobwasUpgrader = gobwas . DefaultUpgrader
// New constructs and returns a new websocket server.
// Listens to incoming connections automatically, no further action is required from the caller.
// The second parameter is the "connHandler", it can be
// filled as `Namespaces`, `Events` or `WithTimeout`, same namespaces and events can be used on the client-side as well,
// Use the `Conn#IsClient` on any event callback to determinate if it's a client-side connection or a server-side one.
//
// See examples for more.
New = neffos . New
2019-07-01 17:50:13 +02:00
// DefaultIDGenerator returns a universal unique identifier for a new connection.
2019-07-05 15:22:20 +02:00
// It's the default `IDGenerator` if missing.
2020-07-10 22:21:09 +02:00
DefaultIDGenerator = func ( ctx * context . Context ) string {
2019-07-05 15:22:20 +02:00
return neffos . DefaultIDGenerator ( ctx . ResponseWriter ( ) , ctx . Request ( ) )
}
2019-07-14 19:22:59 +02:00
2019-07-11 11:59:11 +02:00
// NewRedisStackExchange returns a new redis StackExchange.
// The "channel" input argument is the channel prefix for publish and subscribe.
NewRedisStackExchange = redis . NewStackExchange
2019-07-14 19:22:59 +02:00
// NewNatsStackExchange returns a new nats StackExchange.
// The "url" input argument is the connection string of your nats server.
// The second variadic input argument can be used to use custom `nats.Option`s
// such as authentication and more nats servers addresses.
NewNatsStackExchange = nats . NewStackExchange
// WithNatsOptions can be used as the second input argument of `NewNatsStackExchange`
// to declare a struct-based configuration for the nats server(s).
WithNatsOptions = nats . With
2019-06-02 16:49:45 +02:00
2019-06-15 13:07:35 +02:00
// GorillaDialer is a `Dialer` type for the gorilla/websocket subprotocol implementation.
// Should be used on `Dial` to create a new client/client-side connection.
GorillaDialer = gorilla . Dialer
// GobwasDialer is a `Dialer` type for the gobwas/ws subprotocol implementation.
// Should be used on `Dial` to create a new client/client-side connection.
GobwasDialer = gobwas . Dialer
// DefaultGorillaDialer is a gorilla/websocket dialer with all fields set to the default values.
DefaultGorillaDialer = gorilla . DefaultDialer
// DefaultGobwasDialer is a gobwas/ws dialer with all fields set to the default values.
DefaultGobwasDialer = gobwas . DefaultDialer
2019-06-02 16:49:45 +02:00
// Dial establishes a new websocket client connection.
// Context "ctx" is used for handshake timeout.
// Dialer "dial" can be either `GorillaDialer` or `GobwasDialer`,
// custom dialers can be used as well when complete the `Socket` and `Dialer` interfaces for valid client.
// URL "url" is the endpoint of the websocket server, i.e "ws://localhost:8080/echo".
// The last parameter, and the most important one is the "connHandler", it can be
// filled as `Namespaces`, `Events` or `WithTimeout`, same namespaces and events can be used on the server-side as well.
//
// See examples for more.
Dial = neffos . Dial
2019-06-27 15:28:44 +02:00
// IsTryingToReconnect reports whether the returning "err" from the `Server#Upgrade`
// is from a client that was trying to reconnect to the websocket server.
2019-06-25 21:10:10 +02:00
//
2019-06-27 15:28:44 +02:00
// Look the `Conn#WasReconnected` and `Conn#ReconnectTries` too.
2019-06-25 21:10:10 +02:00
IsTryingToReconnect = neffos . IsTryingToReconnect
2019-07-09 11:16:19 +02:00
// NewStruct returns the `Struct` Conn Handler based on ptr value.
NewStruct = neffos . NewStruct
// JoinConnHandlers combines two or more ConnHandlers as one.
JoinConnHandlers = neffos . JoinConnHandlers
2019-06-02 16:49:45 +02:00
// OnNamespaceConnect is the event name which its callback is fired right before namespace connect,
// if non-nil error then the remote connection's `Conn.Connect` will fail and send that error text.
// Connection is not ready to emit data to the namespace.
OnNamespaceConnect = neffos . OnNamespaceConnect
// OnNamespaceConnected is the event name which its callback is fired after namespace successfully connected.
// Connection is ready to emit data back to the namespace.
OnNamespaceConnected = neffos . OnNamespaceConnected
// OnNamespaceDisconnect is the event name which its callback is fired when
// remote namespace disconnection or local namespace disconnection is happening.
// For server-side connections the reply matters, so if error returned then the client-side cannot disconnect yet,
// for client-side the return value does not matter.
OnNamespaceDisconnect = neffos . OnNamespaceDisconnect // if allowed to connect then it's allowed to disconnect as well.
// OnRoomJoin is the event name which its callback is fired right before room join.
OnRoomJoin = neffos . OnRoomJoin // able to check if allowed to join.
// OnRoomJoined is the event name which its callback is fired after the connection has successfully joined to a room.
OnRoomJoined = neffos . OnRoomJoined // able to broadcast messages to room.
// OnRoomLeave is the event name which its callback is fired right before room leave.
OnRoomLeave = neffos . OnRoomLeave // able to broadcast bye-bye messages to room.
// OnRoomLeft is the event name which its callback is fired after the connection has successfully left from a room.
OnRoomLeft = neffos . OnRoomLeft // if allowed to join to a room, then its allowed to leave from it.
// OnAnyEvent is the event name which its callback is fired when incoming message's event is not declared to the ConnHandler(`Events` or `Namespaces`).
OnAnyEvent = neffos . OnAnyEvent // when event no match.
// OnNativeMessage is fired on incoming native/raw websocket messages.
// If this event defined then an incoming message can pass the check (it's an invalid message format)
// with just the Message's Body filled, the Event is "OnNativeMessage" and IsNative always true.
// This event should be defined under an empty namespace in order this to work.
OnNativeMessage = neffos . OnNativeMessage
// IsSystemEvent reports whether the "event" is a system event,
// OnNamespaceConnect, OnNamespaceConnected, OnNamespaceDisconnect,
// OnRoomJoin, OnRoomJoined, OnRoomLeave and OnRoomLeft.
IsSystemEvent = neffos . IsSystemEvent
// Reply is a special type of custom error which sends a message back to the other side
// with the exact same incoming Message's Namespace (and Room if specified)
// except its body which would be the given "body".
Reply = neffos . Reply
2019-06-06 22:05:17 +02:00
// Marshal marshals the "v" value and returns a Message's Body.
// The "v" value's serialized value can be customized by implementing a `Marshal() ([]byte, error) ` method,
2019-06-03 11:06:18 +02:00
// otherwise the default one will be used instead ( see `SetDefaultMarshaler` and `SetDefaultUnmarshaler`).
// Errors are pushed to the result, use the object's Marshal method to catch those when necessary.
2019-06-06 22:05:17 +02:00
Marshal = neffos . Marshal
2019-06-02 16:49:45 +02:00
)
2019-06-03 11:06:18 +02:00
// SetDefaultMarshaler changes the default json marshaler.
2019-06-06 22:05:17 +02:00
// See `Marshal` package-level function and `Message.Unmarshal` method for more.
2019-06-03 11:06:18 +02:00
func SetDefaultMarshaler ( fn func ( v interface { } ) ( [ ] byte , error ) ) {
neffos . DefaultMarshaler = fn
}
// SetDefaultUnmarshaler changes the default json unmarshaler.
2019-06-06 22:05:17 +02:00
// See `Message.Unmarshal` method and package-level `Marshal` function for more.
2019-06-03 11:06:18 +02:00
func SetDefaultUnmarshaler ( fn func ( data [ ] byte , v interface { } ) error ) {
neffos . DefaultUnmarshaler = fn
}
2019-07-05 15:22:20 +02:00
// IDGenerator is an iris-specific IDGenerator for new connections.
2020-07-10 22:21:09 +02:00
type IDGenerator func ( * context . Context ) string
2019-07-05 15:22:20 +02:00
2020-07-10 22:21:09 +02:00
func wrapIDGenerator ( idGen IDGenerator ) func ( ctx * context . Context ) neffos . IDGenerator {
return func ( ctx * context . Context ) neffos . IDGenerator {
2019-08-03 03:41:09 +02:00
return func ( w http . ResponseWriter , r * http . Request ) string {
return idGen ( ctx )
}
}
}
2019-06-02 16:49:45 +02:00
// Handler returns an Iris handler to be served in a route of an Iris application.
2019-07-05 15:22:20 +02:00
// Accepts the neffos websocket server as its first input argument
// and optionally an Iris-specific `IDGenerator` as its second one.
func Handler ( s * neffos . Server , IDGenerator ... IDGenerator ) context . Handler {
idGen := DefaultIDGenerator
if len ( IDGenerator ) > 0 {
idGen = IDGenerator [ 0 ]
}
2020-07-10 22:21:09 +02:00
return func ( ctx * context . Context ) {
2019-07-05 15:22:20 +02:00
if ctx . IsStopped ( ) {
return
}
2019-08-03 03:41:09 +02:00
Upgrade ( ctx , idGen , s )
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
}
2019-06-02 16:49:45 +02:00
}
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
2019-07-05 15:22:20 +02:00
// Upgrade upgrades the request and returns a new websocket Conn.
// Use `Handler` for higher-level implementation instead.
2020-07-10 22:21:09 +02:00
func Upgrade ( ctx * context . Context , idGen IDGenerator , s * neffos . Server ) * neffos . Conn {
2019-07-05 15:22:20 +02:00
conn , _ := s . Upgrade ( ctx . ResponseWriter ( ) , ctx . Request ( ) , func ( socket neffos . Socket ) neffos . Socket {
return & socketWrapper {
Socket : socket ,
ctx : ctx ,
}
2019-08-03 03:41:09 +02:00
} , wrapIDGenerator ( idGen ) ( ctx ) )
2019-07-05 15:22:20 +02:00
return conn
}
2019-06-02 16:49:45 +02:00
type socketWrapper struct {
neffos . Socket
2020-07-10 22:21:09 +02:00
ctx * context . Context
2019-06-02 16:49:45 +02:00
}
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
2019-06-02 16:49:45 +02:00
// GetContext returns the Iris Context from a websocket connection.
2020-07-10 22:21:09 +02:00
func GetContext ( c * neffos . Conn ) * context . Context {
2019-06-02 16:49:45 +02:00
if sw , ok := c . Socket ( ) . ( * socketWrapper ) ; ok {
return sw . ctx
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
}
2019-06-02 16:49:45 +02:00
return nil
}