From 5dc8cbcefc8d2f3a4db74a2052d85e94dd6b52f0 Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Wed, 25 Apr 2018 13:40:40 +0300 Subject: [PATCH] websocket: expose Connection#Write in favor of https://github.com/kataras/iris/issues/976 Former-commit-id: 74cba9b2bfb4d5cc874eba3330dc9c7bad2ffac5 --- websocket/connection.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/websocket/connection.go b/websocket/connection.go index a38decd7..3c964879 100644 --- a/websocket/connection.go +++ b/websocket/connection.go @@ -149,6 +149,10 @@ type ( // Its connection-relative operations are safe for use. Server() *Server + // Write writes a raw websocket message with a specific type to the client + // used by ping messages and any CloseMessage types. + Write(websocketMessageType int, data []byte) error + // Context returns the (upgraded) context.Context of this connection // avoid using it, you normally don't need it, // websocket has everything you need to authenticate the user BUT if it's necessary @@ -240,6 +244,8 @@ type ( var _ Connection = &connection{} +const CloseMessage = websocket.CloseMessage + func newConnection(ctx context.Context, s *Server, underlineConn UnderlineConnection, id string) *connection { c := &connection{ underline: underlineConn, @@ -271,9 +277,9 @@ func (c *connection) Err() error { return c.err } -// write writes a raw websocket message with a specific type to the client +// 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 { +func (c *connection) Write(websocketMessageType int, data []byte) error { // for any-case the app tries to write from different goroutines, // we must protect them because they're reporting that as bug... c.writerMu.Lock() @@ -295,7 +301,7 @@ func (c *connection) write(websocketMessageType int, data []byte) error { // writeDefault is the same as write but the message type is the configured by c.messageType // if BinaryMessages is enabled then it's raw []byte as you expected to work with protobufs func (c *connection) writeDefault(data []byte) { - c.write(c.messageType, data) + c.Write(c.messageType, data) } const ( @@ -332,7 +338,7 @@ func (c *connection) startPinger() { //fire all OnPing methods c.fireOnPing() // 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 { // must stop to exit the loop and finish the go routine break