Commits

Marcin Lulek  committed 673709b

add client

  • Participants
  • Parent commits ab28262

Comments (0)

Files changed (1)

File gevent_cometd/static/client.js

+define(
+[ "dojo/request", "dojox/socket", "dojox/socket/Reconnect", 'dojo/json',
+        'dojo/query', 'dojox/io/xhrPlugins', 'dojo/_base/unload', 'dojo/topic' ],
+function(Request, Socket, Reconnect, JSON, Query, xhrPlugin, Unload, Topic) {
+    xhrPlugin.addCrossSiteXhr("");
+
+    var jsonp_call = function(args, message) {
+        // The parameters to pass to xhrGet, the url, how to handle it, and the
+        // callbacks.
+        var jsonpArgs = {
+            url : args.url,
+            callbackParamName : "callback",
+            content : {}
+        };
+        var deferred = dojo.io.script.get(jsonpArgs);
+        return deferred;
+    };
+
+    var state = {
+        webapp_url : null,
+        server_url : null,
+        conn_id : null,
+        socket : null,
+        connection_request : null,
+        on_connect_callback : null
+    };
+
+    var message_event = function(event) {
+        for ( var i = 0; i < event.data.length; i++) {
+            var entry = event.data[i];
+            console.log('publishing: ' + 'gevent_cometd/' + entry.type);
+            Topic.publish('gevent_cometd/' + entry.type, entry);
+        }
+    }
+
+    var error_event = function(event) {
+        if (state.socket.reconnect_timout) {
+            clearTimeout(socket.reconnect_timout);
+        }
+        state.socket.reconnect_timout = setTimeout(function() {
+            /* reconnect requires fetching new id and updating socket */
+            Request.post(webapp_url + "/connect", {
+                handleAs : 'json',
+                data : JSON.stringify(state.connection_request),
+                headers : {
+                    "Content-Type" : "application/json"
+                }
+            }).then(
+            function(data) {
+                state.conn_id = data.conn_id;
+                state.socket.args.url = server_url + "/listen?conn_id="
+                + state.conn_id;
+                console.log('reconnected');
+            });
+        }, 500); // try to collapse multiple reconnects into 1
+    }
+
+    var create_socket = function(data) {
+        console.log('attempting to create socket');
+        state.conn_id = data.conn_id;
+        var socket_url = state.server_url + "/listen?conn_id=" + state.conn_id;
+        var socket_conf = {
+            url : socket_url,
+            handleAs : 'json',
+            headers : {
+                "Accept" : "application/json",
+                "Content-Type" : "application/json"
+            }
+        }
+
+        var ws = typeof WebSocket != "undefined";
+        ws = false;
+        if (ws) {
+            console.log('Websocket capable');
+            socket_url = socket_url.replace('https', 'ws');
+            socket_url = socket_url.replace('http', 'ws');
+            state.socket = new Socket(socket_conf);
+        } else {
+            console.log('NOT Websocket capable');
+            if (dojo.isOpera || (dojo.isIE && dojo.isIE < 9)) {
+                socket_conf.transport = jsonp_call;
+            }
+            state.socket = Socket.LongPoll(socket_conf);
+        }
+        // Create socket instance
+        state.socket = Reconnect(state.socket, {
+            reconnectTime : 1000
+        });
+        state.socket.reconnect_timout = null;
+        console.log('created socket with reconnect capability');
+        console.log('adding message event');
+        state.socket.on("message", message_event);
+        console.log('adding error event');
+        state.socket.on("error", error_event);
+        console.log('added events to socket');
+        if (state.on_connect_callback) {
+            state.on_connect_callback(state)
+        }
+    }
+
+    var connect = function(webapp_url, server_url, connection_request,
+    on_connect_callback) {
+        console.log('connecting to get UID of connection');
+        state.connection_request = connection_request;
+        state.webapp_url = webapp_url;
+        state.server_url = server_url;
+        state.on_connect_callback = on_connect_callback;
+        Request.post(state.webapp_url + "/connect", {
+            handleAs : 'json',
+            data : JSON.stringify(state.connection_request),
+            headers : {
+                "Content-Type" : "application/json"
+            }
+        }).then(create_socket);
+    }
+    /* marks connections for GC */
+    Unload.addOnUnload(function() {
+        Request.post(state.server_url + "/disconnect", {
+            handleAs : 'json',
+            data : JSON.stringify({
+                'conn_id' : state.conn_id,
+            }),
+            headers : {
+                "Content-Type" : "application/json"
+            }
+        });
+    });
+
+    return {
+        connect : connect,
+        state : state
+    }
+
+});