doushenmao9036
doushenmao9036
2019-08-02 23:33

如何在Golang中从套接字io获取IP地址

已采纳

I use this code but cant get ip address . How can i get user ip/port address ? I can get socket id with Id(). Below code work well but i cant find how can use more option and function for so variable.

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)
    }
    log.Println("server", server)
    server.On("connection", func(so socketio.Socket) {
        log.Println("so------->>  " ,so  )
        log.Println("id------->>  " , so.Id() )
        log.Println("on connection")

        so.On("chat message", func(msg string) {
            log.Println("emit:", so.Emit("chat message", msg))
            so.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)
    log.Println("Serving at localhost:5555...")
    log.Fatal(http.ListenAndServe(":5555", nil))
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dspym82000 dspym82000 2年前

    Use this expression to get the remote address: so.Request().RemoteAddr

    点赞 评论 复制链接分享

相关推荐