Starting with a working example socket.io(back-end: Python / Flask, front-end: socket.io.js v2.0.3), now I'm trying to configure the client using Go, but I canβt even go through the handshake phase. Apologies for the long post ... (At the end, we also added a Python client that does what I want to implement in Go)
(Next work):
background:
@socketio.on('connect', namespace='/endpoint')
def connect():
print("Client connected with request sid "+request.sid)
@socketio.on('join', namespace='/endpoint')
def join(message):
print("Server received from client:" +message)
print("Client just joined room with request sid "+request.sid)
join_room(request.sid)
front end:
namespace = '/endpoint';
var socket = io.connect(location.protocol + '//' + document.domain + ':' + location.port + namespace);
var client_join_message = "This is a client";
socket.emit('join', client_join_message);
Developer Tools: I notice a few requests until the browser and server want to use webcams and frame sharing:
http:
http:
http:
ws:
http:
http:
Server Logs:
"GET /socket.io/?EIO=3&transport=polling&t=LxcgetJ HTTP/1.1" 200 381 0.000322
Client connected with request sid 025e105a5093467d994a891367380aa3
"POST /socket.io/?EIO=3&transport=polling&t=Lxcgetf&sid=025e105a5093467d994a891367380aa3 HTTP/1.1" 200 219 0.000806
(6450) accepted ('127.0.0.1', 45034)
"GET /socket.io/?EIO=3&transport=polling&t=Lxcgeti&sid=025e105a5093467d994a891367380aa3 HTTP/1.1" 200 227 0.003941
"POST /socket.io/?EIO=3&transport=polling&t=Lxcgetw&sid=025e105a5093467d994a891367380aa3 HTTP/1.1" 200 219 0.001650
"GET /socket.io/?EIO=3&transport=polling&t=Lxcgetx&sid=025e105a5093467d994a891367380aa3 HTTP/1.1" 200 215 0.000235
Server received from client:This is a client
Client just joined room with request sid 025e105a5093467d994a891367380aa3
My attempt with Go (not working):
The code comes from this client example in github.com/graarh/golang-socketio :
package main
import (
"github.com/graarh/golang-socketio"
"github.com/graarh/golang-socketio/transport"
"log"
"runtime"
"time"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
c, err := gosocketio.Dial(
gosocketio.GetUrl("127.0.0.1", 5000, false),
transport.GetDefaultWebsocketTransport())
if err != nil {
log.Fatal(err)
}
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
log.Fatal("Disconnected")
})
if err != nil {
log.Fatal(err)
}
err = c.On(gosocketio.OnConnection, func(h *gosocketio.Channel) {
log.Println("Connected")
})
if err != nil {
log.Fatal(err)
}
time.Sleep(1 * time.Second)
}
Code output result:
Connected
Server Logs:
"GET /socket.io/?EIO=3&transport=websocket HTTP/1.1" 200 0 1.004291
-, ? Go sid, transport=polling... , [go] [socket.io], , ... , .
@John Weldon
:
:
$ go run gotest5.go
2017/10/11 11:21:40 Connected
2017/10/11 11:21:40 result ""
2017/10/11 11:21:40 Done
:
(4380) wsgi starting up on http:
(4380) accepted ('127.0.0.1', 38860)
127.0.0.1 - - [11/Oct/2017 11:21:40] "GET /socket.io/?EIO=3&transport=websocket HTTP/1.1" 200 0 0.003100
, on.('connect'...) on.('join',...) .
Python ( ):
from socketIO_client import SocketIO, BaseNamespace
class ThisNamespace(BaseNamespace):
def on_connect(self):
print('[Connected]')
def on_reconnect(self):
print('[Reconnected]')
def on_disconnect(self):
print('[Disconnected]')
with SocketIO('127.0.0.1', 5000, ThisNamespace) as socketIO:
this_namespace = socketIO.define(ThisNamespace, '/endpoint')
:
python3 test.py
[Connected]
[Disconnected]
[Disconnected]
:
(6047) wsgi starting up on http:
(6047) accepted ('127.0.0.1', 38900)
127.0.0.1 - - [11/Oct/2017 11:53:27] "GET /socket.io/?t=1507712007314-0&transport=polling&EIO=3 HTTP/1.1" 200 381 0.000859
(6047) accepted ('127.0.0.1', 38902)
Client connected with request sid 919ed69264dd4e9f93e7af0294970dbd
Client disconnected with request.sid 919ed69264dd4e9f93e7af0294970dbd
127.0.0.1 - - [11/Oct/2017 11:53:27] "GET /socket.io/?sid=919ed69264dd4e9f93e7af0294970dbd&transport=websocket&EIO=3 HTTP/1.1" 200 0 0.032171