Newbie question: Connection opens, and eventually closes, but no data transfer

36 views
Skip to first unread message

Kevin Cole

unread,
Jun 22, 2011, 3:27:15 PM6/22/11
to pywebsocket
Hi,

I'm trying to see why the following doesn't work. I see the "Socket
opened" and after a small wait the "Socket closed" messages but never
the "Socket messaged". Initially I had something more complicated,
but in an effort to debug, I switched everything to "alert()", and
added the heartbeat in the JavaScript code, and debug statements
and .ws_stream.receive_message call in the Python code. (The HTML is
at http://research.gallaudet.edu/~kjcole/pywebsocket/progress.php but
I'm not seeing how that will impact the problem.) The relevant bits
(I think) are:
______________________________________________________________________

JavaScript:

| var socket = null;
|
| function connect() {
| socket = new WebSocket("ws://research.gallaudet.edu/progress");
| socket.onopen = function () { alert("Socket opened"); };
| socket.onerror = function () { alert("Socket error"); };
| socket.onclose = function (event) { alert("Socket closed"); };
| socket.onmessage = function (event) { alert("Socket messaged"); };
| setInterval(function() { socket.send("Heartbeat"); }, 60000);
| }
|
| window.onload = function() {
| connect();
| }
______________________________________________________________________

Python:

| import time
|
| def web_socket_do_extra_handshake(request):
| pass
|
| def web_socket_transfer_data(request):
| line = request.ws_stream.receive_message() # Must receive a
message?
| debug = open("/tmp/websock.log","a")
| debug.write("Received data transfer request:\n")
| debug.write(line)
| debug.close()
| progress = 0 # Initialize
counter
| while True: # Ad nauseum...
| progress = (progress + 1) % 101 # ...count from 0
to 100
| request.ws_stream.send_message(progress) # ...send progress
back
| time.sleep(0.5) # ...sleep for 0.5
seconds
______________________________________________________________________

Background:

I'm using Ubuntu LTS, mod_pywebsocket-0.6b1, python2.6
(2.6.5-1ubuntu6), apache2 (2.2.14-5ubuntu8.4), and libapache2-mod-
python (3.3.1-8ubuntu2).

The console.html demo with both echo_wsh and bench_wsh are working
both in standalone and under Apache -- albeit with a very short
tolerance for inactivity -- I haven't adjusted apache's configuration:

| RequestReadTimeout header=10-20,minrate=500
| RequestReadTimeout body=10,minrate=500
| KeepAlive On
| MaxKeepAliveRequests 100
| KeepAliveTimeout 15

Yizheng Liao

unread,
Jun 22, 2011, 4:01:13 PM6/22/11
to pyweb...@googlegroups.com
Hi, Kevin:

May I know how you start the standalone.py on the server side?

Thanks,
Bill

Kevin Cole

unread,
Jun 22, 2011, 6:52:54 PM6/22/11
to pyweb...@googlegroups.com
On Wed, Jun 22, 2011 at 16:01, Yizheng Liao <liaoy...@gmail.com> wrote:
Hi, Kevin:

May I know how you start the standalone.py on the server side?

Sure.

$ cd ~/Packages/pywebsocket-0.6b1/src/mod_pywebsocket
$ python2.6 standalone.py \
    -p 4000 \
    -d ~/Packages/misc/pywebsocket-0.6b1/src/example/

Then I opened a browser and pointed it to a copy of the console.html extracted from the package


and fed the form:

ws://localhost:4000/echo

Yizheng Liao

unread,
Jun 23, 2011, 12:11:17 AM6/23/11
to pyweb...@googlegroups.com
Hi, Kevin:

My experience is that you may need to turn on the debug log when you start the standalone.py. The exception of Python will not show up if you do not turn on the debug log.

Please try

$ python2.6 standalone.py \
    -p 4000 \
    -d ~/Packages/misc/pywebsocket-0.6b1/src/example/
    --log-level=debug

See if there are any errors showing up.

Thanks,
Bill

Kevin Cole

unread,
Jun 23, 2011, 1:39:33 PM6/23/11
to pyweb...@googlegroups.com
On Thu, Jun 23, 2011 at 00:11, Yizheng Liao <liaoy...@gmail.com> wrote:
> Hi, Kevin:
> My experience is that you may need to turn on the debug log when you start
> the standalone.py. The exception of Python will not show up if you do not
> turn on the debug log.
> Please try
> $ python2.6 standalone.py \
>     -p 4000 \
>     -d ~/Packages/misc/pywebsocket-0.6b1/src/example/
>     --log-level=debug
> See if there are any errors showing up.

Thanks. That helped a lot.

The problem turned out to be that I was that I passed an integer to
request.ws_stream.send_message and it doesn't want integers. It wants
strings:

| self._write(''.join(['\x00', message.encode('utf-8'), '\xff']))
| AttributeError: web_socket_transfer_data raised
| exception for /progress: 'int' object has no attribute
| 'encode'

So wrapping my value in a str() fixed it... I think. (Now I'm about
to turn off all the debugging code, turn on the actual code, and see
if it works the way I had originally intended.)

Reply all
Reply to author
Forward
0 new messages