我想将此JavaScript(在Node中运行)重写为Go。
let io = require('socket.io-client');
let socket = io('http://botws.generals.io');
socket.on('connect', function() {
console.log('Connected to server.');
});
(我已尝试this library失败;如果有帮助,我可以详细说明我的菜鸟尝试)
答案 0 :(得分:2)
好的,让它运转起来。我必须使用不同的URL。
package main
import (
"github.com/graarh/golang-socketio"
"github.com/graarh/golang-socketio/transport"
"log"
)
func main() {
transport := transport.GetDefaultWebsocketTransport()
ws_url := "ws://botws.generals.io/socket.io/?EIO=3&transport=websocket"
client, err := gosocketio.Dial(ws_url, transport)
if err != nil {
log.Fatal(err)
}
client.On(gosocketio.OnConnection, func(c *gosocketio.Channel, args interface{}) {
log.Println("Connected!")
})
// Block to give client time to connect
select {}
client.Close()
}