K
K
kirill3215922019-10-14 15:23:59
go
kirill321592, 2019-10-14 15:23:59

React native websockets client for golang server?

Wrote the server part of the websocket.io connection on go. And installed base expo react native. How to use react native as a go socket client of a server if go is looking for an html file for the client?

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
  })
  server.OnEvent("/", "notice", func(s socketio.Conn, msg string) {
    fmt.Println("notice:", msg)
    s.Emit("reply", "have "+msg)
  })
  server.OnEvent("/", "msg", func(s socketio.Conn, msg string) string {
    s.SetContext(msg)
    fmt.Println(msg)
    return "recv " + msg
  })
  server.OnEvent("/", "bye", func(s socketio.Conn) string {
    last := s.Context().(string)
    s.Emit("bye", last)
    s.Close()
    return last
  })
  server.OnError("/", func(e error) {
    fmt.Println("meet error:", e)
  })
  server.OnDisconnect("/", func(s socketio.Conn, msg string) {
    fmt.Println("closed", msg)
  })
  go server.Serve()
  defer server.Close()

  http.Handle("/socket.io/", server)
  http.Handle("/", http.FileServer(http.Dir("static")))
  log.Println("Serving")
  log.Fatal(http.ListenAndServe(":8080", nil))
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question