为什么这个 Go 程序建立 WebSocket 连接失败?

huangapple go评论108阅读模式
英文:

Why does the establishment of a websocket connection fail for this Go program?

问题

我正在使用这个IP地址192.168.1.55。我需要将一些数据发送到192.168.1.137。我正在使用以下代码:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "code.google.com/p/go.net/websocket"
  7. )
  8. func Echo(ws *websocket.Conn) {
  9. fmt.Println("Echoing")
  10. for n := 0; n < 10; n++ {
  11. msg := "Hello " + string(n+48)
  12. fmt.Println("Sending to client: " + msg)
  13. err := websocket.Message.Send(ws, msg)
  14. if err != nil {
  15. fmt.Println("Can't send")
  16. break
  17. }
  18. }
  19. }
  20. func main() {
  21. http.Handle("http://192.168.1.137", websocket.Handler(Echo))
  22. http.ListenAndServe(":4242", nil)
  23. }
  24. func checkError(err error) {
  25. if err != nil {
  26. fmt.Println("Fatal error ", err.Error())
  27. os.Exit(1)
  28. }
  29. }

但是我的IP地址无法连接到我上面提到的另一个IP地址(192.168.1.137)。如何解决这个问题?

英文:

I am using this ip 192.168.1.55. I need to send some data to 192.168.1.137. I am using this code

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net/http&quot;
  5. &quot;os&quot;
  6. &quot;code.google.com/p/go.net/websocket&quot;
  7. )
  8. func Echo(ws *websocket.Conn) {
  9. fmt.Println(&quot;Echoing&quot;)
  10. for n := 0; n &lt; 10; n++ {
  11. msg := &quot;Hello &quot; + string(n+48)
  12. fmt.Println(&quot;Sending to client: &quot; + msg)
  13. err := websocket.Message.Send(ws, msg)
  14. if err != nil {
  15. fmt.Println(&quot;Can&#39;t send&quot;)
  16. break
  17. }
  18. }
  19. }
  20. func main() {
  21. http.Handle(&quot;http://192.168.1.137&quot;, websocket.Handler(Echo))
  22. http.ListenAndServe(&quot;:4242&quot;, nil)
  23. }
  24. func checkError(err error) {
  25. if err != nil {
  26. fmt.Println(&quot;Fatal error &quot;, err.Error())
  27. os.Exit(1)
  28. }
  29. }

But my ip is not connecting to the other ip which i have mentioned above(192.168.1.137). How to fix this?

答案1

得分: 1

给定的处理路径是错误的。您必须定义websocket应该连接的路由。

  1. func main() {
  2. http.Handle("/", websocket.Handler(Echo))
  3. http.ListenAndServe(":4242", nil)
  4. }

您可以使用Websocket.org来测试您的代码。

英文:

The path given for handling is wrong. You have to define the route on which the websocket should connect.

  1. func main() {
  2. http.Handle(&quot;http://192.168.1.137&quot;, websocket.Handler(Echo))
  3. http.ListenAndServe(&quot;:4242&quot;, nil)
  4. }

should be

  1. func main() {
  2. http.Handle(&quot;/&quot;, websocket.Handler(Echo))
  3. http.ListenAndServe(&quot;:4242&quot;, nil)
  4. }

You can use Websocket.org to test your code.

答案2

得分: 0

他想要连接,而不是听。

// 你需要确保这些值是正确的,并且服务器正在监听 "192.168.1.137:4242"
origin := "http://192.168.1.55/"
url := "ws://192.168.1.137:4242"

ws, err := websocket.Dial(url, "", origin)
if err != nil {
log.Fatal(err)
}

for n := 0; n < 10; n++ {
msg := "Hello " + strconv.Itoa(n)
fmt.Println("发送给客户端:" + msg)
err := ws.Write([]byte(msg))
if err != nil {
fmt.Println("无法发送")
break
}
}

英文:

He wants to connect to, not to listen.

  1. // you need to make sure this values are correct. and server is listening on &quot;192.168.1.137:4242&quot;
  2. origin := &quot;http://192.168.1.55/&quot;
  3. url := &quot;ws://192.168.1.137:4242&quot;
  4. ws, err := websocket.Dial(url, &quot;&quot;, origin)
  5. if err != nil {
  6. log.Fatal(err)
  7. }
  8. for n := 0; n &lt; 10; n++ {
  9. msg := &quot;Hello &quot; + strconv.Itoa(n)
  10. fmt.Println(&quot;Sending to client: &quot; + msg)
  11. err := ws.Write([]byte(msg))
  12. if err != nil {
  13. fmt.Println(&quot;Can&#39;t send&quot;)
  14. break
  15. }
  16. }

huangapple
  • 本文由 发表于 2015年12月30日 20:48:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/34529871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定