Giter Club home page Giter Club logo

go-socket.io-client's Introduction

go-socket.io-client

go-socket.io-client is an client implementation of socket.io in golang, which is a realtime application framework.

It is compatible with latest implementation of socket.io in node.js, and supports namespace.

Install

Install the package with:

go get github.com/zhouhui8915/go-socket.io-client

Import it with:

import "github.com/zhouhui8915/go-socket.io-client"

and use socketio_client as the package name inside the code.

Example

Please check the example folder for details.

package main

import (
	"bufio"
	"github.com/zhouhui8915/go-socket.io-client"
	"log"
	"os"
)

func main() {

	opts := &socketio_client.Options{
		Transport: "websocket",
		Query:     make(map[string]string),
	}
	opts.Query["user"] = "user"
	opts.Query["pwd"] = "pass"
	uri := "http://192.168.1.70:9090/socket.io/"

	client, err := socketio_client.NewClient(uri, opts)
	if err != nil {
		log.Printf("NewClient error:%v\n", err)
		return
	}

	client.On("error", func() {
		log.Printf("on error\n")
	})
	client.On("connection", func() {
		log.Printf("on connect\n")
	})
	client.On("message", func(msg string) {
		log.Printf("on message:%v\n", msg)
	})
	client.On("disconnection", func() {
		log.Printf("on disconnect\n")
	})

	reader := bufio.NewReader(os.Stdin)
	for {
		data, _, _ := reader.ReadLine()
		command := string(data)
		client.Emit("message", command)
		log.Printf("send message:%v\n", command)
	}
}

License

The 3-clause BSD License - see LICENSE for more details

go-socket.io-client's People

Contributors

etiv avatar hesh915 avatar sebd71 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-socket.io-client's Issues

一些给后来的人的建议

  • 这个版本依赖的类库是 Socket.IO 1.x 的类库,我本人没用 Socket.IO 1.x,不知道能不能使用,不过我肯定的是 Socket.IO 2.x 是有问题的
  • 我这里用 Socket.IO 2.x,参考了 https://socket.io/docs/internals/#socket-io-client 的说明,改了些代码,目前测试可用的,但我不确定是否稳定用于生产(因为我们的需求比较简单)
  • 即使是 1.x,client_conn.go pingLoop 那段代码也是有问题的

error:websocket: bad handshake

Having this issue in a machine with OSX 10.10.5 and the app was compiled in OSX 10.11.5.
It is working correctly on other machines with OSX 10.9.5, 10.10.3, 10.10.5, 10.11.4, 10.11.5

from github.com/zhouhui8915/engine.io-go/websocket/client.go, func NewClient, "resp" is:
&{Status:400 Bad Request StatusCode:400 Proto:HTTP/1.1 ProtoMajor:1 ProtoMinor:1 Header:map[X-Cache-Lookup:[MISS from m8815449E1B30:3128] Via:[1.1 m8815449E1B30 (squid/3.3.5)] Server:[nginx/1.8.0] Date:[Wed, 18 May 2016 13:49:09 GMT] Content-Type:[application/json] Access-Control-Allow-Origin:[*] X-Cache:[MISS from m8815449E1B30]] Body:{Reader:0xc82087d500} ContentLength:-1 TransferEncoding:[] Close:true Trailer:map[] Request:0xc820668c40 TLS:}

from github.com/zhouhui8915/engine.io-go/websocket/client.go, function NewClient, "err" is:
websocket: bad handshake

here is the content of resp.Body:
{"code":3,"message":"Bad request"}

reconnect after disconnect

i need keep a stable connection between go client and node server, but after go client read the message this break teh connection.

2019/03/30 17:10:56 on message:hello1553987456281
2019/03/30 17:10:58 on disconnect

how i keep go client connected?

Attachment?

how to send attachment form the frontend how can i do that please help be with sample examples?

Namespace

Hello
How are you ?

I have a problem.
I can not use the namespace.
Could you give me an example?

I can access root but not socket room.

go-socket.io-client 连接nodejs socket.io

你好 我用go-socket.io-client 连接nodejs socket.io 在下面这段代码报错连 原因不明。
p2 := make([]byte, 4096)
l, err = pack.Read(p2)
if err != nil {
return err
}

报错:2019/03/25 16:08:25 NewClient error:EOF

未知原因在源码中`pack, err = c.getCurrent().NextReader()`处出现问题

opts := &socketio_client.Options{
		Transport: "websocket",
		Query:     make(map[string]string),
	}
	uri := "https://ws.gate.io/socket.io/"

	client, err := socketio_client.NewClient(uri, opts)
	if err != nil {
		panic(err)
		return
	}

按照example,用这个地址连接,返回错误invalid input,我根据源码调试,发现已经获取到了sid等一系列信息。
在client_conn.go文件的325行pack, err = c.getCurrent().NextReader()返回了错误,有点迷惑

Intermittent connection failures

There is a problem with this client that results in connection attempts failing intermittently. This is more easily reproduced by attempting to connect multiple instances of the client to a server.

Here is a gist that has a test to easily reproduce the problem. Just save the file to a directory, make sure you have the required packages, then run go test to see the failure. It will look very similar to the output below.

These packages are needed to run the tests:

go get -u github.com/googollee/go-socket.io
go get -u github.com/graarh/golang-socketio
go get -u github.com/zhouhui8915/go-socket.io-client

Here is the output of the tests:

>go test
2016/11/21 12:10:00 http: response.WriteHeader on hijacked connection
2016/11/21 12:10:00 http: response.WriteHeader on hijacked connection
2016/11/21 12:10:00 http: response.WriteHeader on hijacked connection
2016/11/21 12:10:00 http: response.WriteHeader on hijacked connection
2016/11/21 12:10:00 http: response.WriteHeader on hijacked connection
2016/11/21 12:10:00 http: response.WriteHeader on hijacked connection
2016/11/21 12:10:00 http: response.WriteHeader on hijacked connection
2016/11/21 12:10:00 http: response.WriteHeader on hijacked connection
2016/11/21 12:10:00 http: response.WriteHeader on hijacked connection
2016/11/21 12:10:00 http: response.WriteHeader on hijacked connection
--- FAIL: TestZhouhui (25.00s)
        socketio_test.go:74: [server] Listening on 127.0.0.1:58654
        socketio_test.go:113: [client] #03 connecting...
        socketio_test.go:113: [client] #04 connecting...
        socketio_test.go:113: [client] #07 connecting...
        socketio_test.go:113: [client] #01 connecting...
        socketio_test.go:113: [client] #06 connecting...
        socketio_test.go:113: [client] #05 connecting...
        socketio_test.go:113: [client] #02 connecting...
        socketio_test.go:132: Waiting for client connection attempts to finish
        socketio_test.go:113: [client] #10 connecting...
        socketio_test.go:113: [client] #08 connecting...
        socketio_test.go:113: [client] #09 connecting...
        socketio_test.go:65: [server] Client #01 connected: nu8jEhbmI38vzI_i3rUe
        socketio_test.go:65: [server] Client #02 connected: iBV0WjWVu1s29p3nJAgE
        socketio_test.go:65: [server] Client #03 connected: UeruIAXOHX5ETc8DOpzt
        socketio_test.go:65: [server] Client #04 connected: _XcPUDWA0loqGntf0E3w
        socketio_test.go:65: [server] Client #05 connected: ySSx04spjsNldmy94c0j
        socketio_test.go:65: [server] Client #06 connected: fooWr_iJ2SFQtcQzcMFr
        socketio_test.go:65: [server] Client #07 connected: UeruIAXOHX5ETc8DOpzt
        socketio_test.go:65: [server] Client #08 connected: 1HIbt6xa3QHb1sJng8be
        socketio_test.go:65: [server] Client #09 connected: I0tSRklByiasdszvBgga
        socketio_test.go:65: [server] Client #10 connected: WJF5UYjovQzQpHIkf303
        socketio_test.go:117: [client] #07 could not connect to http://127.0.0.1:58654: EOF
        socketio_test.go:121: [client] #06 connected
        socketio_test.go:121: [client] #02 connected
        socketio_test.go:121: [client] #04 connected
        socketio_test.go:121: [client] #08 connected
        socketio_test.go:121: [client] #03 connected
        socketio_test.go:121: [client] #05 connected
        socketio_test.go:117: [client] #01 could not connect to http://127.0.0.1:58654: EOF
        socketio_test.go:117: [client] #10 could not connect to http://127.0.0.1:58654: EOF
        socketio_test.go:117: [client] #09 could not connect to http://127.0.0.1:58654: EOF
FAIL
exit status 1
FAIL    github.com/dcormier/socket.io-bug       25.072s

The http: response.WriteHeader on hijacked connection messages are caused by a different issue (googollee/go-socket.io#108) and may appear regardless of whether the connection attempts were successful.

Other socket.io clients work reliably in this same scenario. This one fails.

The error being returned, "EOF", is probably the io.EOF value. I suspect that the client is expecting data to be immediately available to be read, but in fact it takes a moment for the server to send it. Sometimes the client gets to that point before the server has sent the data.

The failure almost always takes 25 seconds (the default pingInterval value). There is a second failure that's very rare and returns the same error immediately. I've included notes in the test file on reproducing this one. Very rarely, the test will actually pass. If this happens, just run it again to see it fail.

Invalid Input when creating new client

        opts := &socketio_client.Options{
		Transport: "websocket",
	}

	client, err := socketio_client.NewClient("https://cloud.tech", opts)
	if err != nil {
		log.Printf("NewClient error: %v\n", err)
		return
	}

	client.On("error", func() {
		log.Printf("on error\n")
	})

I am getting NewClient error: invalid input

How can we pass auth params to authenticate the client?

I'll have to convert my node js socket client connection code that is trying to establish connection with server and also authenticating the user passing auth params there. My node js client code looks like this. I want the same functionality in golang.

socket = io('localhost:3000', {
          path: 'https://localhost:3000/socket.io',
          auth: {
            username: 'username',
            token: "abcd",
          },
     });

We can pass query params here in this go-socket.io-client implementation, but how can we pass auth to authenticate the user and achieve the same equivalent functionality as above using this library in go? If it isn't there, kindly add this functionality.

Acknowledgement callback not working because of Line 148: client.go

Acknowledgements callbacks, eg. when you Emit("myMessage", data, func(s string){...})
were not working for me.
I dug into it and found it is because of Line 148 of client.go, which is currently:

case _ACK:
case _BINARY_ACK:
  return nil, client.onAck(packet.Id, decoder, packet)

When an _ACK arrives, client.onAck() is never called.
If I change it to this, it works:

case _ACK:
  return nil, client.onAck(packet.Id, decoder, packet)
case _BINARY_ACK:
  return nil, client.onAck(packet.Id, decoder, packet)

I'm using Go 1.6.0

Maybe it should be like this?

case _ACK, _BINARY_ACK:
  return nil, client.onAck(packet.Id, decoder, packet)

0.9.x

Does it work with 0.9.x socketio?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.