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

Added SendJSON function #3

Open
wants to merge 3 commits 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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ Create instance of `Websocket` by passing url of websocket-server end-point
socket.SendBinary(token)
```

#### Sending JSON Data
```go
json_data := map[string]string {
"Key": "Value"
}
socket.SendJSON(json_data)
```

#### Closing the connection with server
```go
socket.Close()
Expand Down
34 changes: 21 additions & 13 deletions gowebsocket.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package gowebsocket

import (
"github.com/gorilla/websocket"
"net/http"
"errors"
"crypto/tls"
"errors"
"net/http"
"net/url"
"sync"
"github.com/sacOO7/go-logger"
"reflect"
"sync"

"github.com/gorilla/websocket"
logging "github.com/sacOO7/go-logger"
)

type Empty struct {
Expand All @@ -21,7 +22,7 @@ func (socket Socket) EnableLogging() {
}

func (socket Socket) GetLogger() logging.Logger {
return logger;
return logger
}

type Socket struct {
Expand All @@ -32,7 +33,7 @@ type Socket struct {
RequestHeader http.Header
OnConnected func(socket Socket)
OnTextMessage func(message string, socket Socket)
OnBinaryMessage func(data [] byte, socket Socket)
OnBinaryMessage func(data []byte, socket Socket)
OnConnectError func(err error, socket Socket)
OnDisconnected func(err error, socket Socket)
OnPingReceived func(data string, socket Socket)
Expand All @@ -46,7 +47,7 @@ type ConnectionOptions struct {
UseCompression bool
UseSSL bool
Proxy func(*http.Request) (*url.URL, error)
Subprotocols [] string
Subprotocols []string
}

// todo Yet to be done
Expand All @@ -55,7 +56,7 @@ type ReconnectionOptions struct {

func New(url string) Socket {
return Socket{
Url: url,
Url: url,
RequestHeader: http.Header{},
ConnectionOptions: ConnectionOptions{
UseCompression: false,
Expand All @@ -75,7 +76,7 @@ func (socket *Socket) setConnectionOptions() {
}

func (socket *Socket) Connect() {
var err error;
var err error
socket.setConnectionOptions()

socket.Conn, _, err = socket.WebsocketDialer.Dial(socket.Url, socket.RequestHeader)
Expand Down Expand Up @@ -151,22 +152,29 @@ func (socket *Socket) Connect() {
}

func (socket *Socket) SendText(message string) {
err := socket.send(websocket.TextMessage, [] byte (message))
err := socket.send(websocket.TextMessage, []byte(message))
if err != nil {
logger.Error.Println("write:", err)
return
}
}

func (socket *Socket) SendBinary(data [] byte) {
func (socket *Socket) SendBinary(data []byte) {
err := socket.send(websocket.BinaryMessage, data)
if err != nil {
logger.Error.Println("write:", err)
return
}
}

func (socket *Socket) send(messageType int, data [] byte) error {
func (socket *Socket) SendJSON(v interface{}) error {
socket.sendMu.Lock()
err := socket.Conn.WriteJSON(v)
socket.sendMu.Unlock()
return err
}

func (socket *Socket) send(messageType int, data []byte) error {
socket.sendMu.Lock()
err := socket.Conn.WriteMessage(messageType, data)
socket.sendMu.Unlock()
Expand Down