我正在使用这个ip 192.168.1.55。我需要发送一些数据到192.168.1.137。我正在使用这个代码
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)
}
}但是我的ip没有连接到我上面提到的另一个ip (192.168.1.137)。如何解决这个问题?
发布于 2015-12-30 21:28:39
给出的处理路径是错误的。您必须定义连接websocket的路由。
func main() {
http.Handle("http://192.168.1.137", websocket.Handler(Echo))
http.ListenAndServe(":4242", nil)
}应该是
func main() {
http.Handle("/", websocket.Handler(Echo))
http.ListenAndServe(":4242", nil)
}您可以使用Websocket.org测试您的代码。
https://stackoverflow.com/questions/34529871
复制相似问题