Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary event support for writing #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = tab
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
19 changes: 14 additions & 5 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,21 @@ func send(msg *protocol.Message, c *Channel, args interface{}) error {
Create packet based on given data and send it
*/
func (c *Channel) Emit(method string, args interface{}) error {
msg := &protocol.Message{
Type: protocol.MessageTypeEmit,
Method: method,
}
switch args.(type) {
case []byte:
if err := c.conn.WriteBinaryMessage(method, args.([]byte)); err != nil {
return err
}

return nil
default:
msg := &protocol.Message{
Type: protocol.MessageTypeEmit,
Method: method,
}

return send(msg, c, args)
return send(msg, c, args)
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type Connection interface {
*/
WriteMessage(message string) error

/**
Send binary message with event, block until sent
*/
WriteBinaryMessage(event string, message []byte) error

/**
Close current connection
*/
Expand Down
25 changes: 25 additions & 0 deletions transport/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/gorilla/websocket"
"io/ioutil"
"net/http"
"sync"
"time"
)

Expand All @@ -26,6 +27,8 @@ var (
ErrorHttpUpgradeFailed = errors.New("Http upgrade failed")
)

var socketMu sync.Mutex

type WebsocketConnection struct {
socket *websocket.Conn
transport *WebsocketTransport
Expand Down Expand Up @@ -58,6 +61,9 @@ func (wsc *WebsocketConnection) GetMessage() (message string, err error) {
}

func (wsc *WebsocketConnection) WriteMessage(message string) error {
socketMu.Lock()
defer socketMu.Unlock()

wsc.socket.SetWriteDeadline(time.Now().Add(wsc.transport.SendTimeout))
writer, err := wsc.socket.NextWriter(websocket.TextMessage)
if err != nil {
Expand All @@ -73,6 +79,25 @@ func (wsc *WebsocketConnection) WriteMessage(message string) error {
return nil
}

func (wsc *WebsocketConnection) WriteBinaryMessage(event string, message []byte) error {
socketMu.Lock()
defer socketMu.Unlock()

wsc.socket.SetWriteDeadline(time.Now().Add(wsc.transport.SendTimeout))

if err := wsc.socket.WriteMessage(websocket.TextMessage, []byte(`451-["` + event + `",{"_placeholder":true,"num":0}]`)); err != nil {
return err
}

newMessage := []byte{4}
newMessage = append(newMessage, message...)
if err := wsc.socket.WriteMessage(websocket.BinaryMessage, newMessage); err != nil {
return err
}

return nil
}

func (wsc *WebsocketConnection) Close() {
wsc.socket.Close()
}
Expand Down