Websocket: added OnPing to Connection

see issue #825

Former-commit-id: c7c97d40e352f4e550ea96eb482e71c3c50c8c09
This commit is contained in:
Gerson Alexander Pardo Gamez 2017-12-01 17:48:37 -05:00 committed by GitHub
parent 4818d184b1
commit 80b86136f4

View File

@ -131,6 +131,8 @@ type (
// MessageFunc is the second argument to the Emitter's Emit functions. // MessageFunc is the second argument to the Emitter's Emit functions.
// A callback which should receives one parameter of type string, int, bool or any valid JSON/Go struct // A callback which should receives one parameter of type string, int, bool or any valid JSON/Go struct
MessageFunc interface{} MessageFunc interface{}
// PingFunc is the callback which fires each ping
PingFunc func()
// Connection is the front-end API that you will use to communicate with the client side // Connection is the front-end API that you will use to communicate with the client side
Connection interface { Connection interface {
// Emitter implements EmitMessage & Emit // Emitter implements EmitMessage & Emit
@ -154,6 +156,8 @@ type (
OnDisconnect(DisconnectFunc) OnDisconnect(DisconnectFunc)
// OnError registers a callback which fires when this connection occurs an error // OnError registers a callback which fires when this connection occurs an error
OnError(ErrorFunc) OnError(ErrorFunc)
// OnPing registers a callback which fires on each ping
OnPing(PingFunc)
// FireStatusCode can be used to send a custom error message to the connection // FireStatusCode can be used to send a custom error message to the connection
// //
// It does nothing more than firing the OnError listeners. It doesn't sends anything to the client. // It does nothing more than firing the OnError listeners. It doesn't sends anything to the client.
@ -200,6 +204,7 @@ type (
onDisconnectListeners []DisconnectFunc onDisconnectListeners []DisconnectFunc
onRoomLeaveListeners []LeaveRoomFunc onRoomLeaveListeners []LeaveRoomFunc
onErrorListeners []ErrorFunc onErrorListeners []ErrorFunc
onPingListeners []PingFunc
onNativeMessageListeners []NativeMessageFunc onNativeMessageListeners []NativeMessageFunc
onEventListeners map[string][]MessageFunc onEventListeners map[string][]MessageFunc
// these were maden for performance only // these were maden for performance only
@ -305,6 +310,8 @@ func (c *connection) startPinger() {
// verifies if already disconected // verifies if already disconected
break break
} }
//fire all OnPing methods
c.fireOnPing()
// try to ping the client, if failed then it disconnects // try to ping the client, if failed then it disconnects
err := c.write(websocket.PingMessage, []byte{}) err := c.write(websocket.PingMessage, []byte{})
if err != nil { if err != nil {
@ -430,6 +437,10 @@ func (c *connection) OnError(cb ErrorFunc) {
c.onErrorListeners = append(c.onErrorListeners, cb) c.onErrorListeners = append(c.onErrorListeners, cb)
} }
func (c *connection) OnPing(cb PingFunc) {
c.onPingListeners = append(c.onPingListeners, cb)
}
func (c *connection) FireOnError(errorMessage string) { func (c *connection) FireOnError(errorMessage string) {
for _, cb := range c.onErrorListeners { for _, cb := range c.onErrorListeners {
cb(errorMessage) cb(errorMessage)
@ -492,6 +503,13 @@ func (c *connection) fireOnLeave(roomName string) {
} }
} }
func (c *connection) fireOnPing() {
// fire the onPingListeners
for i := range c.onPingListeners {
c.onPingListeners[i]()
}
}
func (c *connection) Disconnect() error { func (c *connection) Disconnect() error {
return c.server.Disconnect(c.ID()) return c.server.Disconnect(c.ID())
} }