1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-20 03:17:04 +00:00

Another new feature: websocket controller, for real

Former-commit-id: c1a59b86733e890709b52446e22427a17d87f5fc
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-20 17:56:28 +02:00
parent b78698f6c0
commit 2042fddb66
10 changed files with 281 additions and 61 deletions

View File

@@ -137,6 +137,9 @@ type (
Connection interface {
// Emitter implements EmitMessage & Emit
Emitter
// Err is not nil if the upgrader failed to upgrade http to websocket connection.
Err() error
// ID returns the connection's identifier
ID() string
@@ -181,6 +184,11 @@ type (
// Note: the callback(s) called right before the server deletes the connection from the room
// so the connection theoretical can still send messages to its room right before it is being disconnected.
OnLeave(roomLeaveCb LeaveRoomFunc)
// Wait starts the pinger and the messages reader,
// it's named as "Wait" because it should be called LAST,
// after the "On" events IF server's `Upgrade` is used,
// otherise you don't have to call it because the `Handler()` does it automatically.
Wait()
// Disconnect disconnects the client, close the underline websocket conn and removes it from the conn list
// returns the error, if any, from the underline connection
Disconnect() error
@@ -197,6 +205,7 @@ type (
}
connection struct {
err error
underline UnderlineConnection
id string
messageType int
@@ -207,6 +216,7 @@ type (
onPingListeners []PingFunc
onNativeMessageListeners []NativeMessageFunc
onEventListeners map[string][]MessageFunc
started bool
// these were maden for performance only
self Emitter // pre-defined emitter than sends message to its self client
broadcast Emitter // pre-defined emitter that sends message to all except this
@@ -237,6 +247,7 @@ func newConnection(ctx context.Context, s *Server, underlineConn UnderlineConnec
onErrorListeners: make([]ErrorFunc, 0),
onNativeMessageListeners: make([]NativeMessageFunc, 0),
onEventListeners: make(map[string][]MessageFunc, 0),
started: false,
ctx: ctx,
server: s,
}
@@ -252,6 +263,11 @@ func newConnection(ctx context.Context, s *Server, underlineConn UnderlineConnec
return c
}
// Err is not nil if the upgrader failed to upgrade http to websocket connection.
func (c *connection) Err() error {
return c.err
}
// write writes a raw websocket message with a specific type to the client
// used by ping messages and any CloseMessage types.
func (c *connection) write(websocketMessageType int, data []byte) error {
@@ -322,6 +338,13 @@ func (c *connection) startPinger() {
}()
}
func (c *connection) fireOnPing() {
// fire the onPingListeners
for i := range c.onPingListeners {
c.onPingListeners[i]()
}
}
func (c *connection) startReader() {
conn := c.underline
hasReadTimeout := c.server.config.ReadTimeout > 0
@@ -503,11 +526,20 @@ func (c *connection) fireOnLeave(roomName string) {
}
}
func (c *connection) fireOnPing() {
// fire the onPingListeners
for i := range c.onPingListeners {
c.onPingListeners[i]()
// Wait starts the pinger and the messages reader,
// it's named as "Wait" because it should be called LAST,
// after the "On" events IF server's `Upgrade` is used,
// otherise you don't have to call it because the `Handler()` does it automatically.
func (c *connection) Wait() {
if c.started {
return
}
c.started = true
// start the ping
c.startPinger()
// start the messages reader
c.startReader()
}
func (c *connection) Disconnect() error {