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))
}
4
  • 1
    can you share te error message Commented Sep 10, 2019 at 1: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 Commented Sep 10, 2019 at 1:14
  • @novalagung i edited my question....please read it again Commented Sep 10, 2019 at 1:16
  • @novalagung i use v1.4 but i need to use v1.0 Commented Sep 10, 2019 at 1:22

1 Answer 1

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/[email protected]

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

Sign up to request clarification or add additional context in comments.

2 Comments

it didnot work . go: cannot use path@version syntax in GOPATH mode
@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"

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.