--
Best regards, Viacheslav Chumushuk
email/jabber: vo...@root.ua
Ukraine, Khmelnitsky
package main
import (
"http"
"io"
"websocket"
)
// This is the guy that handle the WebSocket comunication.
func EchoServer(ws *websocket.Conn) {
io.Copy(ws, ws);
}
func main() {
// Map the url "/echo" the your websocket EchoServer function
http.Handle("/echo", websocket.Handler(EchoServer));
// Start the httpServer from http package
err := http.ListenAndServe(":12345", nil);
if err != nil {
panic("ListenAndServe: " + err.String())
}
}
Also,
Avoid "!" on the subject. :)
--
André Moraes
http://andredevchannel.blogspot.com/
In that sample case this is just an echo server.
Basically this copy everything from the input stream of the ws and put
on the output stream.
You should read the data from ws (it implements io.Reader).
Make your processing and write to ws (it also implements io.Writer).
func EchoServer(ws *websocket.Conn) {
input := make([]byte, 100);
for {
sz, err := ws.Reader(input)
ws.Write([]byte(fmt.Sprintf("You sent: %i", sz)))
}
}
In this new version, the system loops on the for and read at most 100
bytes from ws.
After reading, it send the message "You sent: <number of bytes read>"
to the client.
instead of
You sent: %i