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

How to access a server based on namespaces and query criteria #44

Open
mengzhalin opened this issue Feb 22, 2019 · 1 comment
Open

How to access a server based on namespaces and query criteria #44

mengzhalin opened this issue Feb 22, 2019 · 1 comment

Comments

@mengzhalin
Copy link

mengzhalin commented Feb 22, 2019

I now have a client for node.js implementation.code show as below:
`var url = require('url'),
util = require('util'),
server_url = url.parse('127.0.0.1:8080'),
socket = require('socket.io-client')(url.format({
protocol: server_url.protocol,
hostname: server_url.hostname,
port: server_url.port,
pathname: 'test.io',
query: {
identify: 'Data',
key: 'b290e8b3f35343ac9d275265'
}
}));

socket.on('connect', function () {
util.log('plpc.io connected');
var buf = new Buffer(39);
socket.emit('test-msg', buf);
}I am writing the client with graarh/golang-socketio, the code is as follows:var Url string
//Url = "ws://192.0.0.1:8080/socket.io/plpc.io?EIO=3&identify=Data&key=b290e8b3f35343ac9d275265&transport=websocket"

str_identify := "Data"
str_key := "b290e8b3f35343ac9d275265"
var params []string
params = append(params, fmt.Sprintf("identify=%s", str_identify))
params = append(params, fmt.Sprintf("key=%s", str_key))

Url = gosocketio.GetUrl("127.0.0.1",8080,params,false)
fmt.Println(Url)
client,err := gosocketio.Dial(Url,"test.io",transport.GetDefaultWebsocketTransport())
if err != nil{
	//panic(err)
	log.Fatal(err)
	return
}

client.On(gosocketio.OnConnection,func(h *gosocketio.Channel){
	fmt.Println("Connected")
	
})
client.On(gosocketio.OnDisconnection,func(h *gosocketio.Channel){
	fmt.Println("Disconnected")
})
client.On(gosocketio.OnError,func(h *gosocketio.Channel){
	fmt.Println("Error")
})
time.Sleep(1 * time.Second)
for{
	select{
		case <-bDataByte:
			fmt.Println(<-bDataByte)
			
			result, err := client.Ack("test-msg", <-bDataByte, time.Second*1)
			if err != nil {
				log.Fatal(err)
			}else {
				log.Println("Ack result to /join: ", result)
			}
			//Data := <-gItems
			//fmt.Println(Data)
			//err := client.Emit("test-msg", <-bDataByte)
			//if err != nil {
			//	log.Fatal(err)
			//}
			//time.Sleep(10 * time.Millisecond)
			break
		default:
			time.Sleep(10 * time.Millisecond)
	}
}
client.Close()`

The current situation is that the client written by node.js can work normally. The client written by golang can connect to the server, but the server cannot parse the data correctly.I modified two functions in the socket.io-client library.
`/**
Get ws/wss url by host and port
*/
func GetUrl(host string, port int, params []string, secure bool) string {
var prefix string
if secure {
prefix = webSocketSecureProtocol
} else {
prefix = webSocketProtocol
}
//return prefix + host + ":" + strconv.Itoa(port) + socketioUrl
_url, err := url.Parse(prefix + host + ":" + strconv.Itoa(port) + socketioUrl)
if err != nil {
fmt.Println("We unable to parse given url: ", _url)
}

if len(params) > 0 {
    _uval := _url.Query()
    for _, element := range params {
        s := strings.Split(element, "=")
        _uval.Add(s[0], s[1])
    }
    _url.RawQuery = _uval.Encode()
}
return _url.String()

}

/**
connect to host and initialise socket.io protocol

The correct ws protocol url example:
ws://myserver.com/socket.io/?EIO=3&transport=websocket

You can use GetUrlByHost for generating correct url
*/
func Dial(url string, nsp string, tr transport.Transport) (*Client, error) {
c := &Client{}
c.initChannel()
c.initMethods()

var err error
c.conn, err = tr.Connect(url)
if err != nil {
	return nil, err
}

nspMsg := fmt.Sprintf("4%d%s", protocol.MessageTypeOpen, nsp)
c.conn.WriteMessage(nspMsg)

go inLoop(&c.Channel, &c.methods)
go outLoop(&c.Channel, &c.methods)
go pinger(&c.Channel)

return c, nil

}`
why is the client written by GO not working properly? And, what should be the correct way to write?

@mengzhalin
Copy link
Author

@graarh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant