From fc0948d7ad308a47a80139a0779e08f24903df46 Mon Sep 17 00:00:00 2001 From: fmoga Date: Sun, 24 Jul 2011 13:32:46 +0000 Subject: Migrate to embedded Jetty as websocket server. Add javascript service proxy injection. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1150357 13f79535-47bb-0310-9956-ffa450edef68 --- .../main/resources/js/TuscanyWebsocketToolkit.js | 51 ++++++++++++++++++++++ .../src/main/resources/js/jquery.json-2.2.min.js | 45 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js create mode 100644 sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/jquery.json-2.2.min.js (limited to 'sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js') diff --git a/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js b/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js new file mode 100644 index 0000000000..501250f3cf --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/TuscanyWebsocketToolkit.js @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +var channels = []; + +function buildRequest(operation, message) { + return $.toJSON({ + operation: operation, + payload: $.toJSON(message) + }); +} + +function sendMessage(port, operation, message) { + if (!channels[port]) { + ws = new WebSocket("ws://" + window.location.hostname + ":" + port); + channels[port] = ws; + ws.onopen = function() { + $(document).trigger('' + port); + } + ws.onmessage = function (message) { + var response = $.evalJSON(message.data); + eval('Tuscany.WebsocketComponentContext.' + response.operation + '.responseHandler(' + response.payload + ')'); + } + } + + var jsonReq = buildRequest(operation, message); + + if (ws.readyState == WebSocket.CONNECTING) { + $(document).bind('' + port, jsonReq, function(event) { + ws.send(event.data); + }); + } else if (ws.readyState == WebSocket.OPEN) { + ws.send(jsonReq); + } +} \ No newline at end of file diff --git a/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/jquery.json-2.2.min.js b/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/jquery.json-2.2.min.js new file mode 100644 index 0000000000..b176b7ace5 --- /dev/null +++ b/sca-java-2.x/contrib/modules/binding-websocket/src/main/resources/js/jquery.json-2.2.min.js @@ -0,0 +1,45 @@ +/* + * jQuery JSON Plugin + * version: 2.2 (2009-08-14) + * + * This document is licensed as free software under the terms of the + * MIT License: http://www.opensource.org/licenses/mit-license.php + * + * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org + * website's http://www.json.org/json2.js, which proclaims: + * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that + * I uphold. + * + * It is also influenced heavily by MochiKit's serializeJSON, which is + * copyrighted 2005 by Bob Ippolito. + */ +(function($){$.toJSON=function(o) +{if(typeof(JSON)=='object'&&JSON.stringify) +return JSON.stringify(o);var type=typeof(o);if(o===null) +return"null";if(type=="undefined") +return undefined;if(type=="number"||type=="boolean") +return o+"";if(type=="string") +return $.quoteString(o);if(type=='object') +{if(typeof o.toJSON=="function") +return $.toJSON(o.toJSON());if(o.constructor===Date) +{var month=o.getUTCMonth()+1;if(month<10)month='0'+month;var day=o.getUTCDate();if(day<10)day='0'+day;var year=o.getUTCFullYear();var hours=o.getUTCHours();if(hours<10)hours='0'+hours;var minutes=o.getUTCMinutes();if(minutes<10)minutes='0'+minutes;var seconds=o.getUTCSeconds();if(seconds<10)seconds='0'+seconds;var milli=o.getUTCMilliseconds();if(milli<100)milli='0'+milli;if(milli<10)milli='0'+milli;return'"'+year+'-'+month+'-'+day+'T'+ +hours+':'+minutes+':'+seconds+'.'+milli+'Z"';} +if(o.constructor===Array) +{var ret=[];for(var i=0;i