Reputation: 978
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?
Upvotes: 0
Views: 92
Reputation: 1500
He wants to connect to, not to listen.
// you need to make sure this values are correct. and server is listening on "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("Sending to client: " + msg)
err := ws.Write([]byte(msg))
if err != nil {
fmt.Println("Can't send")
break
}
}
Upvotes: 0
Reputation: 723
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.
Upvotes: 1