Skip to content

fix json.Unmarshal for int64 precision loss issue #49

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

Open
wants to merge 2 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Examples directory contains simple client and server.

### Installation

go get github.com/graarh/golang-socketio
go get github.com/moliqingwa/golang-socketio

### Simple server usage

Expand All @@ -30,7 +30,7 @@ Examples directory contains simple client and server.
}

//handle custom event
server.On("send", func(c *gosocketio.Channel, msg Message) string {
server.On("send", func(c *gosocketio.Channel, msg string) string {
//send event to all in room
c.BroadcastTo("chat", "message", msg)
return "OK"
Expand Down Expand Up @@ -77,7 +77,7 @@ var socket = io('ws://yourdomain.com', {transports: ['websocket']});
// --- caller is default handlers

//on connection handler, occurs once for each connected client
server.On(gosocketio.OnConnection, func(c *gosocketio.Channel, args interface{}) {
server.On(gosocketio.OnConnection, func(c *gosocketio.Channel, args string) {
//client id is unique
log.Println("New client connected, client id is ", c.Id())

Expand Down
8 changes: 4 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package gosocketio

import (
"github.com/graarh/golang-socketio/transport"
"github.com/moliqingwa/golang-socketio/transport"
"strconv"
)

const (
webSocketProtocol = "ws://"
webSocketProtocol = "ws://"
webSocketSecureProtocol = "wss://"
socketioUrl = "/socket.io/?EIO=3&transport=websocket"
socketioUrl = "/socket.io/?EIO=3&transport=websocket"
)

/**
Expand All @@ -21,7 +21,7 @@ type Client struct {

/**
Get ws/wss url by host and port
*/
*/
func GetUrl(host string, port int, secure bool) string {
var prefix string
if secure {
Expand Down
4 changes: 2 additions & 2 deletions examples/client.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"github.com/graarh/golang-socketio"
"github.com/graarh/golang-socketio/transport"
"github.com/moliqingwa/golang-socketio"
"github.com/moliqingwa/golang-socketio/transport"
"log"
"runtime"
"time"
Expand Down
4 changes: 2 additions & 2 deletions examples/server.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"github.com/graarh/golang-socketio"
"github.com/graarh/golang-socketio/transport"
"github.com/moliqingwa/golang-socketio"
"github.com/moliqingwa/golang-socketio/transport"
"log"
"net/http"
"time"
Expand Down
41 changes: 27 additions & 14 deletions handler.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package gosocketio

import (
"encoding/json"
"github.com/graarh/golang-socketio/protocol"
"sync"
"github.com/moliqingwa/golang-socketio/protocol"
"log"
"reflect"
"sync"
)

const (
Expand All @@ -27,6 +27,8 @@ type methods struct {

onConnection systemHandler
onDisconnection systemHandler

serialize Serialize
}

/**
Expand Down Expand Up @@ -93,18 +95,19 @@ func (m *methods) processIncomingMessage(c *Channel, msg *protocol.Message) {
return
}

if !f.ArgsPresent {
f.callFunc(c, &struct{}{})
return
}
if f.ArgsPresent {
data := f.getArgs()
err := m.serialize.Unmarshal([]byte(msg.Args), &data)
if err != nil {
return
}

data := f.getArgs()
err := json.Unmarshal([]byte(msg.Args), &data)
if err != nil {
return
f.callFunc(c, data)
} else {
f.callFunc(c, &struct{}{})
}

f.callFunc(c, data)
return

case protocol.MessageTypeAckRequest:
f, ok := m.findMethod(msg.Method)
Expand All @@ -116,7 +119,7 @@ func (m *methods) processIncomingMessage(c *Channel, msg *protocol.Message) {
if f.ArgsPresent {
//data type should be defined for unmarshall
data := f.getArgs()
err := json.Unmarshal([]byte(msg.Args), &data)
err := m.serialize.Unmarshal([]byte(msg.Args), &data)
if err != nil {
return
}
Expand All @@ -130,7 +133,17 @@ func (m *methods) processIncomingMessage(c *Channel, msg *protocol.Message) {
Type: protocol.MessageTypeAckResponse,
AckId: msg.AckId,
}
send(ack, c, result[0].Interface())

res := result[0].Interface()
if res != nil {
if json, err := m.serialize.Marshal(&res); err == nil {
send(ack, c, string(json))
} else {
log.Fatalf("marshal failed, raw=%+v, err=%+v\n", res, err)
}
} else {
send(ack, c, "")
}

case protocol.MessageTypeAckResponse:
waiter, err := c.ack.getWaiter(msg.AckId)
Expand Down
4 changes: 2 additions & 2 deletions loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package gosocketio
import (
"encoding/json"
"errors"
"github.com/graarh/golang-socketio/protocol"
"github.com/graarh/golang-socketio/transport"
"github.com/moliqingwa/golang-socketio/protocol"
"github.com/moliqingwa/golang-socketio/transport"
"net/http"
"sync"
"time"
Expand Down
18 changes: 6 additions & 12 deletions send.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package gosocketio

import (
"encoding/json"
"errors"
"github.com/graarh/golang-socketio/protocol"
"github.com/moliqingwa/golang-socketio/protocol"
"log"
"time"
)
Expand All @@ -16,21 +15,16 @@ var (
/**
Send message packet to socket
*/
func send(msg *protocol.Message, c *Channel, args interface{}) error {
func send(msg *protocol.Message, c *Channel, args string) error {
//preventing json/encoding "index out of range" panic
defer func() {
if r := recover(); r != nil {
log.Println("socket.io send panic: ", r)
}
}()

if args != nil {
json, err := json.Marshal(&args)
if err != nil {
return err
}

msg.Args = string(json)
if args != "" {
msg.Args = args
}

command, err := protocol.Encode(msg)
Expand All @@ -50,7 +44,7 @@ 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 {
func (c *Channel) Emit(method string, args string) error {
msg := &protocol.Message{
Type: protocol.MessageTypeEmit,
Method: method,
Expand All @@ -62,7 +56,7 @@ func (c *Channel) Emit(method string, args interface{}) error {
/**
Create ack packet based on given data and send it and receive response
*/
func (c *Channel) Ack(method string, args interface{}, timeout time.Duration) (string, error) {
func (c *Channel) Ack(method string, args string, timeout time.Duration) (string, error) {
msg := &protocol.Message{
Type: protocol.MessageTypeAckRequest,
AckId: c.ack.getNextId(),
Expand Down
6 changes: 6 additions & 0 deletions serialize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gosocketio

type Serialize interface {
Marshal(v interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
}
12 changes: 6 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/graarh/golang-socketio/protocol"
"github.com/graarh/golang-socketio/transport"
"github.com/moliqingwa/golang-socketio/protocol"
"github.com/moliqingwa/golang-socketio/transport"
"math/rand"
"net/http"
"sync"
Expand Down Expand Up @@ -43,7 +43,7 @@ type Server struct {

/**
Close current channel
*/
*/
func (c *Channel) Close() {
if c.server != nil {
closeChannel(c, &c.server.methods)
Expand Down Expand Up @@ -193,7 +193,7 @@ func (s *Server) List(room string) []*Channel {

}

func (c *Channel) BroadcastTo(room, method string, args interface{}) {
func (c *Channel) BroadcastTo(room, method string, args string) {
if c.server == nil {
return
}
Expand All @@ -203,7 +203,7 @@ func (c *Channel) BroadcastTo(room, method string, args interface{}) {
/**
Broadcast message to all room channels
*/
func (s *Server) BroadcastTo(room, method string, args interface{}) {
func (s *Server) BroadcastTo(room, method string, args string) {
s.channelsLock.RLock()
defer s.channelsLock.RUnlock()

Expand All @@ -222,7 +222,7 @@ func (s *Server) BroadcastTo(room, method string, args interface{}) {
/**
Broadcast to all clients
*/
func (s *Server) BroadcastToAll(method string, args interface{}) {
func (s *Server) BroadcastToAll(method string, args string) {
s.sidsLock.RLock()
defer s.sidsLock.RUnlock()

Expand Down