Websocket eof problem

1,031 views
Skip to first unread message

ja...@okonski.org

unread,
Feb 16, 2012, 5:41:08 AM2/16/12
to golan...@googlegroups.com
Hello again,

So i'm trying to make a simple command forwarder to connect my home computer to a server I own, so that I can push commands to my server and my home pc gets it. Those commands are simple pause/resume for my downloader. My design is, that on a server, I run a hub instance, which creates a websocket server for backend (I call it backend window) and another websocket server for future browser requests. I'm trying to make it work by setting a hub first (which listens for a backend connection and client connections). I'm bounding those two instances with a channel, they run concurrently on ListenAndServe(). When a client connects and sends a message to the hub, it gets streamed through a channel to backend window and then to the real backend (on my home pc). When backend responds to the backend window on the hub, the hub prints the result for the client.

The best I could come up with is that the first message from a client gets passed and parsed in Interpret() method (on the backend - my home pc) and it actually works as intended. Later commands though get an EOF error in the backend instance (again, home pc) and they loop forever. I've tried closing the connection or responding with empty strings to the hub, but I don't really know how to handle an EOF on a websocket (there are only Read() and Write() methods).

The fact that only the first message gets through I've achieved by setting 0 as a http timeout for both read and write on backend window and hub interface.

I've put up the code on https://github.com/farnoy/gosab/tree/eof_problem/src/gosab, hope it's structured enough. Command code is in cmd/commands.go and handlers are in hub/handlers and backend/connector packages.

To compile the source code, put the git repo in GOPATH, then just do `go build gosab/cmd`, run it by issuing:
./cmd -mode="hub" -backend=":8082" -hub=":8083"
./cmd -mode="backend" -hub="localhost:8082/" # hub option here gives the address to backend window

To test it from a browser, execute this javascript in your webkit inspector/firebug:
var s = new WebSocket("ws://localhost:8083")
s.send("1 5")

This is the output from the hub command (first) that I'm aiming for:

got 1 5
sending  1 5  to backend
Backend says:  Sabnzbd paused for 5 minutes

When "Backend says: x" matches anything from backend/interpreter, it means that it works.

Thank you for your time.

Jessta

unread,
Feb 16, 2012, 7:53:45 AM2/16/12
to ja...@okonski.org, golan...@googlegroups.com
On Thu, Feb 16, 2012 at 9:41 PM, <ja...@okonski.org> wrote:
> The best I could come up with is that the first message from a client gets
> passed and parsed in Interpret() method (on the backend - my home pc) and it
> actually works as intended. Later commands though get an EOF error in the
> backend instance (again, home pc) and they loop forever. I've tried closing
> the connection or responding with empty strings to the hub, but I don't
> really know how to handle an EOF on a websocket (there are only Read() and
> Write() methods).

Both Read() and Write() return error values, you should be checking for these.
They satisfy the io.Reader and io.Writer interfaces, you should read
the docs for them to see how they act. It's a bit different to how
most other methods work.
http://golang.org/pkg/io/#Reader
http://golang.org/pkg/io/#Writer

Read() will return an eof when the websocket is closed by the client.

> I've put up the code
> on https://github.com/farnoy/gosab/tree/eof_problem/src/gosab, hope it's
> structured enough. Command code is in cmd/commands.go and handlers are in
> hub/handlers and backend/connector packages.

I had a brief look at this code and you're ignoring a lot of error
return values, if you don't do proper error handling then you can't
expect anything to work properly.

--
=====================
http://jessta.id.au

ja...@okonski.org

unread,
Feb 16, 2012, 1:28:40 PM2/16/12
to golan...@googlegroups.com
I've solved the problem by using pure websocket handlers for a server, not functions returning closures as a handler.
Reply all
Reply to author
Forward
0 new messages