Garrett
Garrett

Reputation: 4394

Example of simple websocket connection from client

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)

Upvotes: 2

Views: 1459

Answers (1)

Garrett
Garrett

Reputation: 4394

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()
}

Upvotes: 2

Related Questions