Websocket: callback not functioning after window.locaiton=

#1

Hi, I have a backend application written in python+flask_socketio.
I connect with this backend with “websocket” (not regular simple TCP socket).
since I cant have any async operations (might be wrong conclusion after reading several posts on ‘threads’), I used javascript in HtmlDialog to connect to my backend websocket app.

every time I send a message from javascript, my server will send a message back,
without line 29:"windows.locaion=skp:xxxxxx
Sending and Receiving worked perfectly.

if I add line 29, and send to the server again, the server can still receive the message, but the javascript ‘handle_message’ will not trigger, unless I close the socket and reconnect again.

I suspect after window.location=skp:xxxxx, the page reloads and all instances are changed. however, when I print out the socket.id prior and after ‘window.location=skp:xxxxx’, they are the same.

even more, if I type ‘window.location=skp:xxxxx’ from the html console manually, not only can I trigger the skp function correctly, but also when I send to the socket again, I can still get the correct response.

questions are:
1 what is the proper way to drive sketchup with websocket messages?
2 can I connet websocket in ruby asynchronously?

0 Likes

#2

You are using WebDialog (which uses the system’s browser engine) instead of the modern HtmlDialog. Does Internet Explorer support WebSockets at all? (It seems yes, but with problems.)

Have you tried HtmlDialog?

0 Likes

#3

Please post code correctly on forum, not images from your editor …

SEE: [How to] Colourize code on the forum? (wiki)

0 Likes

#4

Hi Dan, thanks for the pointer, let me try:


    var msgs=[]
    socket=io('ws://localhost:5000/',false)

    handle_connect=function(data){
        console.log('connected');
        // socket.send('hello server')
    }
    handle_message=function(data){
        console.log('recieved:'+data)
        var msg='skp:add_boxes@'+data;
        console.log(msg);
        window.location=msg;
    }

    socket.on('connect',handle_connect)
    socket.on('message',handle_message)

    var send=function(data){
        socket.send(data)
    }

1 Like

#5
  1. yes, as the question states, HTMLDialog was used.
  2. yes, Edge,Chrome,Forefox and the HTMLDialog in my system all support WebSocket, and I have been using this technique to bridges other applications such as web app, Rhino, and Unity.
    However, I have absolutely no idea how to make it work in sketchup.
0 Likes

#6

No, as the code and the topic title mention window.location = "skp:…", you either created the dialog as UI::WebDialog, or you used window.location in a UI::HtmlDialog where it is out of place and does not work. Then you need to call the Ruby callbacks directly as functions on the external JavaScript object sketchup:

sketchup.add_boxes(data)
2 Likes

#7

OMG! I didn’t know i can call a sketchup call back like this! it totally opened a new world to me! Thank you very much!

0 Likes

#8

This also works in htmldialog actually.

1 Like