doubi7346 2015-12-30 12:48
浏览 153

为什么此Go程序的websocket连接建立失败?

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

package main

import (
    "fmt"
    "net/http"
    "os"
    "code.google.com/p/go.net/websocket"
)

func Echo(ws *websocket.Conn) {
    fmt.Println("Echoing")

    for n := 0; n < 10; n++ {
        msg := "Hello  " + string(n+48)
        fmt.Println("Sending to client: " + msg)
        err := websocket.Message.Send(ws, msg)
        if err != nil {
            fmt.Println("Can't send")
            break
        }
    }
}

func main() {

    http.Handle("http://192.168.1.137", websocket.Handler(Echo))
    http.ListenAndServe(":4242", nil)
}

func checkError(err error) {
    if err != nil {
        fmt.Println("Fatal error ", err.Error())
        os.Exit(1)
    }
}

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

  • 写回答

2条回答 默认 最新

  • dpfln86244 2015-12-30 13:28
    关注

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

    func main() {
        http.Handle("http://192.168.1.137", websocket.Handler(Echo))
        http.ListenAndServe(":4242", nil)
    }
    

    should be

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

    You can use Websocket.org to test your code.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题