I want to send data from Android to Golang with socket.io . I do that with Nodejs correctly But now , i want do with Go. I cant find simple example.how can i do that?
Asked
Active
Viewed 1,567 times
1 Answers
6
I assume you would like to use a Go implementation of the Socket.IO server library instead of the standard Node.js one. If so, you can try googollee/go-socket.io project. Here is an example from the project page:
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))
}

Andrey Dyatlov
- 1,628
- 1
- 10
- 12
-
1dear i do that . but i want to have android studio example code. – m-tech Jan 02 '19 at 09:49
-
4@m-tech, googollee/go-socket.io implements the same protocol as the Node.js does, so there are no specific examples for it. So, the [standard android implementation](https://socket.io/blog/native-socket-io-and-android/) should work. – Andrey Dyatlov Jan 02 '19 at 11:31