4

My Go version is:

go version go1.12.9 windows/amd64

From this link:

https://github.com/googollee/go-socket.io/tree/v1.0

I run this code in cmd:

go get github.com/googollee/go-socket.io

But i cant run this code :

package main

import (
    "log"
    "net/http"

    "github.com/googollee/go-socket.io"
)

func main() {
    server, err := socketio.NewServer(nil)
    if err != nil {
        log.Fatal(err)
    }
    server.On("connection", func(so socketio.Socket) {
        log.Println("on connection")
        so.Join("chat")
        so.On("chat message", func(msg string) {
            log.Println("emit:", so.Emit("chat message", msg))
            server.BroadcastTo("chat", "chat message", msg)
        })
        so.On("disconnection", func() {
            log.Println("on disconnect")
        })
    })
    server.On("error", func(so socketio.Socket, err error) {
        log.Println("error:", err)
    })

    http.Handle("/socket.io/", server)
    http.Handle("/", http.FileServer(http.Dir("./asset")))
    log.Println("Serving at localhost:5000...")
    log.Fatal(http.ListenAndServe(":5000", nil))
}

I cant run it , why?

I think my problem is in getting V1.4 and v1.0

go get github.com/googollee/go-socket.io

how can i get v1.0 for Golang from github?

But this code work well

package main

import (
    "fmt"
    "log"
    "net/http"
    "github.com/googollee/go-socket.io"
)

func main() {
    server, err := socketio.NewServer(nil)
    if err != nil {
        log.Fatal(err)
    }
    server.OnConnect("/", func(s socketio.Conn) error {
        s.SetContext("")
        fmt.Println("connected:", s.ID())
        return nil
    })



    http.Handle("/socket.io/", server)
    http.Handle("/", http.FileServer(http.Dir("./asset")))
    log.Println("Serving at localhost:8000...")
    log.Fatal(http.ListenAndServe(":8000", nil))
}
melina
  • 195
  • 1
  • 8
  • 1
    can you share te error message – novalagung Sep 10 '19 at 01:07
  • .\main2.go:15:11: server.On undefined (type *socketio.Server has no field or method On) .\main2.go:15:37: undefined: socketio.Socket .\main2.go:20:19: server.BroadcastTo undefined (type *socketio.Server has no field or method BroadcastTo) .\main2.go:26:11: server.On undefined (type *socketio.Server has no field or method On) .\main2.go:26:32: undefined: socketio.Socket – melina Sep 10 '19 at 01:14
  • @novalagung i edited my question....please read it again – melina Sep 10 '19 at 01:16
  • @novalagung i use v1.4 but i need to use v1.0 – melina Sep 10 '19 at 01:22

1 Answers1

2

If your project uses go modules (has a go.mod file, after a go mod init myproject), all you might need to do is:

go get  github.com/googollee/go-socket.io@v1.0.1

I use v1.0.1 because there is no 1.0.0 tag in the googollee/go-socket.io releases

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • it didnot work . go: cannot use path@version syntax in GOPATH mode – melina Sep 10 '19 at 09:53
  • @melina make sure you are using the latest Go (1.13) and that you have a go.mod (`go mod init myproject`) first: that will be enough to *not* be in the "GOPATH mode" – VonC Sep 10 '19 at 10:06