Here. Just post a link to this thread.

https://github.com/web2py/web2py/issues/new

On Wednesday, 15 July 2015 18:51:37 UTC-5, Samuel Sowah wrote:
>
>
>
> On Wednesday, July 15, 2015 at 11:46:38 PM UTC, Massimo Di Pierro wrote:
>>
>> you may be right. Will check asap. Please open a ticket.
>>
>> I've never opened a ticket, where and how do I do that?
>  
>
>> On Wednesday, 15 July 2015 18:17:00 UTC-5, Samuel Sowah wrote:
>>>
>>> I believe this is a bug. Please confirm what I'm thinking or correct me 
>>> if I'm wrong.
>>> The web2py_websocket function in web2py.js takes 4 parameters (2 
>>> optional, onopen and onclose)
>>> web2py_websocket: function (url, onmessage, onopen, onclose) {
>>>       if("WebSocket" in window) {
>>>         var ws = new WebSocket(url);
>>>         ws.onopen = onopen ? onopen : (function () {});
>>>         ws.onmessage = onmessage;
>>>         ws.onclose = onclose ? onclose : (function () {});
>>>         return true; /* supported */
>>>       } else return false; /* not supported */
>>>     },
>>>
>>>
>>> I used all 4 parameters but only 3 behave how I expect them except to. 
>>> url, onmessage, and onopen.
>>> the url connects the client to the websocket, onmessage executes when a 
>>> message is sent, onopen if specified executes when the socket is opened. I 
>>> expect that onclose would execute if specified, when the socket is closed 
>>> (client disconnects). for some reason, when the client disconnects, the 
>>> function doesn't execute. I've already asked leading questions in other 
>>> threads but I think this should sum up my exact problem.
>>>
>>> *default/home.html*
>>> <script>
>>>    $(document).ready(function(){
>>>       var data;
>>>       $.web2py.web2py_websocket('ws://127.0.0.1:8888/realtime/home',
>>>               function(e){data=eval('('+e.data+')')},
>>>               function(){ajax('someone_online',[],'');},
>>>               function(){ajax('{{='someone_offline/%s' % auth.user.id
>>> }}',[],'');});
>>>    });
>>> </script>
>>>
>>>
>>> *default.py*
>>> @auth.requires_login()
>>> def someone_online():
>>>     if not db(db.online_users.user_id==auth.user.id).select():
>>>         db.online_users.insert(user_id=auth.user.id)
>>>     script2="""$('#userStatus').prepend('<div>%s just came 
>>> online</div>').fadeIn('slow').slideDown('slow')""" % auth.user.full_name
>>>     websocket_send('http://127.0.0.1:8888', script2, 'somekey', 'home')
>>>     
>>> script1="""$('#usersOnline').html($('<span>%s</span>').fadeIn('slow').slideDown('slow'))"""
>>>  
>>> % len(db().select(db.online_users.user_id))
>>>     websocket_send('http://127.0.0.1:8888', script1, 'somekey', 'home')
>>>
>>>
>>> @auth.requires_login()
>>> def someone_offline():
>>>     db(db.online_users.user_id==request.args(0)).delete()
>>>     script2="""$('#userStatus').append('<div>%s just went 
>>> offline</div>').fadeIn('slow').slideDown('slow')""" % db(db.auth_user.id
>>> ==request.args(0)).select().first().full_name
>>>     websocket_send('http://127.0.0.1:8888', script2, 'somekey', 'home')
>>>     
>>> script1="""$('#usersOnline').html($('<span>%s</span>').fadeIn('slow').slideDown('slow'))"""
>>>  
>>> % len(db().select(db.online_users.user_id))
>>>     websocket_send('http://127.0.0.1:8888', script1, 'somekey', 'home')
>>>
>>>
>>> Scenario for the code above. when someone opens home.html, the page will 
>>> connect to the websocket server because of the ws:// url on the page. 
>>> because the onopen parameter has been specified, that function would 
>>> execute immediately after the connection is established, which is to call 
>>> the someone_online method in default.py via ajax. the someone_online method 
>>> then via websocket_send updates the page with the statement "<user> just 
>>> came online".
>>>
>>> This so far works just as expected.
>>> 1436999804.04:CONNECT to home
>>> 1436999804.41:MESSAGE to home:$('#userStatus').prepend('<div>saso just 
>>> came online</div>').fadeIn('slow').slideDown('slow')
>>> 1436999804.41:MESSAGE to 
>>> home:$('#usersOnline').html($('<span>2</span>').fadeIn('slow').slideDown('slow'))
>>>
>>>
>>> The part that doesn't work is, when the page is closed, i expect that 
>>> other people who have that same page opened will get the update "<user> 
>>> just went offline". the onclose function doesn't seem to be executed at 
>>> all, or at least it doesn't do what I'm expecting. For example, when i 
>>> refresh the page, it disconnects and reconnects without sending the offline 
>>> message.
>>> 1437002425.12:DISCONNECT from home
>>> 1437002425.52:CONNECT to home
>>> 1437002425.98:MESSAGE to home:$('#userStatus').prepend('<div>saso just 
>>> came online</div>').fadeIn('slow').slideDown('slow')
>>> 1437002425.98:MESSAGE to 
>>> home:$('#usersOnline').html($('<span>2</span>').fadeIn('slow').slideDown('slow'))
>>>
>>>
>>> What I have tried:
>>> I tried starting the websocket server with the token option -t. When 
>>> this is in play, somehow due to unregistered tokens I believe, the 
>>> connection opens and closes immediately due to this code in the 
>>> websocket_messaging.py script.
>>> if DistributeHandler.tokens:
>>>     if not self.token in tokens or not token[self.token] is None:
>>>         self.close()
>>>     else:
>>>         tokens[self.token] = self
>>>
>>> When the connection closes because of this code above checking for 
>>> tokens, the "onclose" function somehow executes
>>> 1436999086.05:CONNECT to home
>>> 1436999086.05:DISCONNECT from home
>>> 1436999086.57:MESSAGE to home:$('#userStatus').prepend('<div>saso just 
>>> came online</div>').fadeIn('slow').slideDown('slow')
>>> 1436999086.6:MESSAGE to 
>>> home:$('#usersOnline').html($('<span>2</span>').fadeIn('slow').slideDown('slow'))
>>> 1436999087.49:MESSAGE to home:$('#userStatus').append('<div>saso just 
>>> went offline</div>').fadeIn('slow').slideDown('slow')
>>> 1436999087.49:MESSAGE to 
>>> home:$('#usersOnline').html($('<span>1</span>').fadeIn('slow').slideDown('slow'))
>>>
>>> but because the disconnection has already happened, other 
>>> tabs/clients/browsers that have opened the home.html page do not get 
>>> updated.
>>>
>>> How can I get the onclose function to work as expected, or how do I 
>>> achieve what I want properly? Please help.
>>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to