Need a simple Websocket server client example!

2,002 views
Skip to first unread message

nvcnvn

unread,
May 20, 2011, 6:26:25 AM5/20/11
to golang-nuts
I have try the code in the http://golang.org/pkg/websocket/#Handler
And http://golang.org/pkg/websocket/#Conn.Dial example

But any time just the server can run, some error with the client!

Can I have a simple example that:
1. The client send a message: "Tom"
2. The server receive the message and say: "Hi Tom!"
3. In the client windows I can see: "Server just said: Hi Tom!"

Thanks for your time!

Viacheslav Chumushuk

unread,
May 20, 2011, 8:15:46 AM5/20/11
to golang-nuts
Hi.
I was used net package and TCP listener and it works just fine.
Here is my code: https://gist.github.com/982795

--
Best regards, Viacheslav Chumushuk
email/jabber: vo...@root.ua
Ukraine, Khmelnitsky

André Moraes

unread,
May 20, 2011, 9:58:05 AM5/20/11
to golang-nuts
Adapted from http://golang.org/pkg/websocket/#Handler.ServeHTTP

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/

nvcnvn

unread,
May 21, 2011, 4:56:22 AM5/21/11
to golang-nuts
I quite of confuse at:
func EchoServer(ws *websocket.Conn) {
io.Copy(ws, ws);
}

Now it reply the same message as the client sent, how to I modify it?


On May 20, 8:58 pm, André Moraes <andr...@gmail.com> wrote:
> Adapted fromhttp://golang.org/pkg/websocket/#Handler.ServeHTTP

Martin Capitanio

unread,
May 21, 2011, 7:57:29 AM5/21/11
to golan...@googlegroups.com
> Now it reply the same message as the client sent, how to I modify it? 


func pingpong(ws *websocket.Conn) {
        defer func() {
                log.Println("ws connection closed")
                ws.Close()
        }()
        log.Println("ws connection established")
        buf := make([]byte, 1024)
        msg := "hi client"
        for {
                _, err := ws.Write([]byte(msg))
                log.Println(">", msg)
                if err != nil {
                        log.Println(err)
                        break
                }
                n, err := ws.Read(buf)
                if err != nil {
                        log.Println(err)
                        break
                }
                log.Println("<", string(buf[0:n]))
        }
}

nvcnvn

unread,
May 22, 2011, 9:22:26 AM5/22/11
to golang-nuts
Hi Martin.

Can you help me by explain your code?
Why I thought this look like the client code rather than server?

André Moraes

unread,
May 23, 2011, 2:58:22 PM5/23/11
to nvcnvn, golang-nuts
On Sat, May 21, 2011 at 5:56 AM, nvcnvn <nvc...@gmail.com> wrote:
> I quite of confuse at:
> func EchoServer(ws *websocket.Conn) {
>        io.Copy(ws, ws);
> }
>
> Now it reply the same message as the client sent, how to I modify it?

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.

André Moraes

unread,
May 23, 2011, 2:59:12 PM5/23/11
to golang-nuts
The correct format is:
You sent: %d

instead of
You sent: %i

nvcnvn

unread,
May 24, 2011, 9:39:02 AM5/24/11
to golang-nuts
OK, thanks you a lot André :)
Reply all
Reply to author
Forward
0 new messages