2

I would like to rewrite this JavaScript (run in Node) into Go.

let io = require('socket.io-client');
let socket = io('http://botws.generals.io');

socket.on('connect', function() {
    console.log('Connected to server.');
});

(I've tried this library unsuccessfully; I can elaborate on my noob attempts if that helps)

1 Answer 1

2

Ok, got it working. There was a different URL I had to use.

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()
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was trying to do this using the base net library. And gorilla. I'll try this when I get home. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.