summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2011-08-29 10:49:15 -0500
committerJoel Martin <github@martintribe.org>2011-08-29 10:49:15 -0500
commit4be643b29c1146b085e6688d4850e935a165da09 (patch)
tree23265e91f68553f07ea5c2ec31059e94bf807f57
parentfa9e686e386a7a774bfd3ad50cbbb526603cd399 (diff)
parented0622a890de811eb6d766d5c5a9a62d726f0593 (diff)
downloadweb-socket-js-4be643b29c1146b085e6688d4850e935a165da09.tar.gz
Merge remote branch 'gimite/master' into hybi-08
Conflicts: WebSocketMain.swf WebSocketMainInsecure.zip
-rw-r--r--.gitignore1
-rw-r--r--README.md68
-rw-r--r--flash-src/.gitignore1
-rw-r--r--flash-src/build.properties.sample2
-rwxr-xr-xflash-src/build.sh6
-rw-r--r--flash-src/build.xml66
-rw-r--r--flash-src/net/gimite/websocket/IWebSocketLogger.as (renamed from flash-src/IWebSocketLogger.as)5
-rw-r--r--flash-src/net/gimite/websocket/WebSocket.as (renamed from flash-src/WebSocket.as)14
-rw-r--r--flash-src/net/gimite/websocket/WebSocketEvent.as (renamed from flash-src/WebSocketEvent.as)2
-rw-r--r--flash-src/net/gimite/websocket/WebSocketMain.as (renamed from flash-src/WebSocketMain.as)2
-rw-r--r--flash-src/net/gimite/websocket/WebSocketMainInsecure.as (renamed from flash-src/WebSocketMainInsecure.as)4
-rw-r--r--web_socket.js45
12 files changed, 160 insertions, 56 deletions
diff --git a/.gitignore b/.gitignore
index 80e8f7a..ecf5763 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
test.html
+WebSocket.swc
diff --git a/README.md b/README.md
index 239e381..bb6db68 100644
--- a/README.md
+++ b/README.md
@@ -3,8 +3,10 @@
Assuming you have Web server (e.g. Apache) running at **http://example.com/** .
1. Download [web-socket-ruby](http://github.com/gimite/web-socket-ruby/tree/master).
-2. Run sample Web Socket server (echo server) in example.com with: (#1)
- $ ruby web-socket-ruby/samples/echo_server.rb example.com 10081
+2. Run sample Web Socket server (echo server) in example.com with: (#1)<br>
+```
+$ ruby web-socket-ruby/samples/echo_server.rb example.com 10081
+```
3. If your server already provides socket policy file at port **843**, modify the file to allow access to port **10081**. Otherwise you can skip this step. See below for details.
4. Publish the web-socket-js directory with your Web server (e.g. put it in ~/public_html).
5. Change ws://localhost:10081 to **ws://example.com:10081** in sample.html.
@@ -19,29 +21,31 @@ Assuming you have Web server (e.g. Apache) running at **http://example.com/** .
- Copy swfobject.js, web_socket.js, WebSocketMain.swf to your application directory.
- Write JavaScript code:
- <!-- Import JavaScript Libraries. -->
- <script type="text/javascript" src="swfobject.js"></script>
- <script type="text/javascript" src="web_socket.js"></script>
-
- <script type="text/javascript">
-
- // Let the library know where WebSocketMain.swf is:
- WEB_SOCKET_SWF_LOCATION = "WebSocketMain.swf";
-
- // Write your code in the same way as for native WebSocket:
- var ws = new WebSocket("ws://example.com:10081/");
- ws.onopen = function() {
- ws.send("Hello"); // Sends a message.
- };
- ws.onmessage = function(e) {
- // Receives a message.
- alert(e.data);
- };
- ws.onclose = function() {
- alert("closed");
- };
-
- </script>
+```html
+<!-- Import JavaScript Libraries. -->
+<script type="text/javascript" src="swfobject.js"></script>
+<script type="text/javascript" src="web_socket.js"></script>
+
+<script type="text/javascript">
+
+ // Let the library know where WebSocketMain.swf is:
+ WEB_SOCKET_SWF_LOCATION = "WebSocketMain.swf";
+
+ // Write your code in the same way as for native WebSocket:
+ var ws = new WebSocket("ws://example.com:10081/");
+ ws.onopen = function() {
+ ws.send("Hello"); // Sends a message.
+ };
+ ws.onmessage = function(e) {
+ // Receives a message.
+ alert(e.data);
+ };
+ ws.onclose = function() {
+ alert("closed");
+ };
+
+</script>
+```
- Put Flash socket policy file to your server unless you use web-socket-ruby or em-websocket as your WebSocket server. See "Flash socket policy file" section below for details.
@@ -65,13 +69,15 @@ and use Developer Tools (Chrome/Safari) or Firebug (Firefox) to see if console.l
3. Make sure you do NOT open your HTML page as local file e.g. file:///.../sample.html. web-socket-js doesn't work on local file. Open it via Web server e.g. http:///.../sample.html.
-4. If you are NOT using web-socket-ruby as your WebSocket server, you need to place Flash socket policy file on your server. See "Flash socket policy file" section below for details.
+4. Make sure you host your HTML page and WebSocketMain.swf in the same domain. Otherwise, see "How to host HTML file and SWF file in different domains" section.
-5. Check if sample.html bundled with web-socket-js works.
+5. If you are NOT using web-socket-ruby or em-websocket as your WebSocket server, you need to place Flash socket policy file on your server. See "Flash socket policy file" section below for details.
-6. Make sure the port used for WebSocket (10081 in example above) is not blocked by your server/client's firewall.
+6. Check if sample.html bundled with web-socket-js works.
-7. Install [debugger version of Flash Player](http://www.adobe.com/support/flashplayer/downloads.html) to see Flash errors.
+7. Make sure the port used for WebSocket (10081 in example above) is not blocked by your server/client's firewall.
+
+8. Install [debugger version of Flash Player](http://www.adobe.com/support/flashplayer/downloads.html) to see Flash errors.
## Supported environments
@@ -97,9 +103,9 @@ It may or may not work on other browsers such as Safari, Opera or IE 6. Patch fo
This implementation uses Flash's socket, which means that your server must provide Flash socket policy file to declare the server accepts connections from Flash.
-If you use [web-socket-ruby](http://github.com/gimite/web-socket-ruby/tree/master) or [em-websocket](https://github.com/igrigorik/em-websocket), you don't need anything special, because web-socket-ruby handles Flash socket policy file request. But if you already provide socket policy file at port **843**, you need to modify the file to allow access to Web Socket port, because it precedes what web-socket-ruby provides.
+If you use [web-socket-ruby](http://github.com/gimite/web-socket-ruby/tree/master) or [em-websocket](https://github.com/igrigorik/em-websocket), you don't need anything special, because they handle Flash socket policy file request. But if you already provide socket policy file at port **843**, you need to modify the file to allow access to Web Socket port, because it precedes what the libraries provide.
-If you use other Web Socket server implementation, you need to provide socket policy file yourself. See [Setting up A Flash Socket Policy File](http://www.lightsphere.com/dev/articles/flash_socket_policy.html) for details and sample script to run socket policy file server. [node.js implementation is available here](http://github.com/LearnBoost/Socket.IO-node/blob/master/lib/socket.io/transports/flashsocket.js).
+If you use other Web Socket server implementation, you need to provide socket policy file yourself. See [Setting up A Flash Socket Policy File](http://www.lightsphere.com/dev/articles/flash_socket_policy.html) for details and sample script to run socket policy file server. [node.js implementation is available here](https://github.com/3rd-Eden/FlashPolicyFileServer).
Actually, it's still better to provide socket policy file at port 843 even if you use web-socket-ruby or em-websocket. Flash always try to connect to port 843 first, so providing the file at port 843 makes startup faster.
diff --git a/flash-src/.gitignore b/flash-src/.gitignore
new file mode 100644
index 0000000..56fb545
--- /dev/null
+++ b/flash-src/.gitignore
@@ -0,0 +1 @@
+build.properties
diff --git a/flash-src/build.properties.sample b/flash-src/build.properties.sample
new file mode 100644
index 0000000..6ae8786
--- /dev/null
+++ b/flash-src/build.properties.sample
@@ -0,0 +1,2 @@
+# Point this to your Flex SDK directory.
+FLEX_HOME=/usr/local/share/flex_sdk_4
diff --git a/flash-src/build.sh b/flash-src/build.sh
index 598674a..0fb02a5 100755
--- a/flash-src/build.sh
+++ b/flash-src/build.sh
@@ -1,10 +1,12 @@
#!/bin/sh
+# A script to build WebSocketMain.swf and WebSocketMainInsecure.zip.
+
# You need Flex 4 SDK:
# http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4
-mxmlc -static-link-runtime-shared-libraries -target-player=10.0.0 -output=../WebSocketMain.swf WebSocketMain.as &&
-mxmlc -static-link-runtime-shared-libraries -output=../WebSocketMainInsecure.swf WebSocketMainInsecure.as &&
+mxmlc -static-link-runtime-shared-libraries -target-player=10.0.0 -output=../WebSocketMain.swf -source-path=. net/gimite/websocket/WebSocketMain.as &&
+mxmlc -static-link-runtime-shared-libraries -output=../WebSocketMainInsecure.swf -source-path=. net/gimite/websocket/WebSocketMainInsecure.as &&
cd .. &&
zip WebSocketMainInsecure.zip WebSocketMainInsecure.swf &&
rm WebSocketMainInsecure.swf
diff --git a/flash-src/build.xml b/flash-src/build.xml
new file mode 100644
index 0000000..9f42796
--- /dev/null
+++ b/flash-src/build.xml
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+ Ant build file which provides Yet another way (other than build.sh) to build SWF files.
+
+ You need to copy build.properties.sample to build.properties and change FLEX_HOME
+ for your environment.
+-->
+
+<project name="ant" basedir="." default="swf">
+
+<property file="build.properties" /><!-- set sdk -->
+<echo message="Using SDK version: ${FLEX_HOME}" />
+<!-- do not change next 2 lines, it adds needed task to ant -->
+<property name="FLEXTASKS" value="${FLEX_HOME}/ant/lib/flexTasks.jar"/>
+<taskdef resource="flexTasks.tasks" classpath="${FLEXTASKS}"/>
+
+<target name="swf">
+ <delete file="../WebSocketMain.swf"/>
+ <mxmlc file="net/gimite/websocket/WebSocketMain.as"
+ output="../WebSocketMain.swf"
+ incremental="false"
+ strict="true"
+ accessible="false"
+ fork="true"
+ static-link-runtime-shared-libraries="true">
+ <optimize>true</optimize>
+ <target-player>10</target-player>
+ <raw-metadata></raw-metadata>
+ <source-path path-element="."/>
+ </mxmlc>
+</target>
+
+<target name="swfInsecure">
+ <delete file="../WebSocketMainInsecure.swf" />
+ <mxmlc file="net/gimite/websocket/WebSocketMainInsecure.as"
+ output="../WebSocketMainInsecure.swf"
+ incremental="false"
+ strict="true"
+ accessible="false"
+ fork="true"
+ static-link-runtime-shared-libraries="true">
+ <optimize>true</optimize>
+ <target-player>10</target-player>
+ <raw-metadata></raw-metadata>
+ <source-path path-element="."/>
+ </mxmlc>
+</target>
+
+<target name="swc" ><!-- for pure .as -->
+ <delete file="../WebSocket.swc" />
+ <compc output="../WebSocket.swc"
+ incremental="false"
+ strict="true"
+ accessible="false"
+ fork="true"
+ static-link-runtime-shared-libraries="false">
+ <optimize>true</optimize>
+ <raw-metadata></raw-metadata>
+ <include-sources dir="." includes="**/*.as"/>
+ <source-path path-element="."/>
+ </compc>
+</target>
+
+</project>
+ \ No newline at end of file
diff --git a/flash-src/IWebSocketLogger.as b/flash-src/net/gimite/websocket/IWebSocketLogger.as
index 24f4ef7..c3384f3 100644
--- a/flash-src/IWebSocketLogger.as
+++ b/flash-src/net/gimite/websocket/IWebSocketLogger.as
@@ -1,4 +1,7 @@
-package {
+// Copyright: Hiroshi Ichikawa <http://gimite.net/en/>
+// License: New BSD License
+
+package net.gimite.websocket {
public interface IWebSocketLogger {
function log(message:String):void;
diff --git a/flash-src/WebSocket.as b/flash-src/net/gimite/websocket/WebSocket.as
index 63ee5af..3bafd63 100644
--- a/flash-src/WebSocket.as
+++ b/flash-src/net/gimite/websocket/WebSocket.as
@@ -3,7 +3,7 @@
// Reference: http://dev.w3.org/html5/websockets/
// Reference: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07
-package {
+package net.gimite.websocket {
import com.adobe.net.proxies.RFC2817Socket;
import com.gsolo.encryption.MD5;
@@ -292,9 +292,11 @@ public class WebSocket extends EventDispatcher {
private function onSocketIoError(event:IOErrorEvent):void {
var message:String;
if (readyState == CONNECTING) {
- message = "cannot connect to Web Socket server at " + url + " (IoError)";
+ message = "cannot connect to Web Socket server at " + url + " (IoError: " + event.text + ")";
} else {
- message = "error communicating with Web Socket server at " + url + " (IoError)";
+ message =
+ "error communicating with Web Socket server at " + url +
+ " (IoError: " + event.text + ")";
}
onError(message);
}
@@ -303,10 +305,12 @@ public class WebSocket extends EventDispatcher {
var message:String;
if (readyState == CONNECTING) {
message =
- "cannot connect to Web Socket server at " + url + " (SecurityError)\n" +
+ "cannot connect to Web Socket server at " + url + " (SecurityError: " + event.text + ")\n" +
"make sure the server is running and Flash socket policy file is correctly placed";
} else {
- message = "error communicating with Web Socket server at " + url + " (SecurityError)";
+ message =
+ "error communicating with Web Socket server at " + url +
+ " (SecurityError: " + event.text + ")";
}
onError(message);
}
diff --git a/flash-src/WebSocketEvent.as b/flash-src/net/gimite/websocket/WebSocketEvent.as
index 598eeb2..aa17cac 100644
--- a/flash-src/WebSocketEvent.as
+++ b/flash-src/net/gimite/websocket/WebSocketEvent.as
@@ -1,4 +1,4 @@
-package {
+package net.gimite.websocket {
import flash.events.Event;
diff --git a/flash-src/WebSocketMain.as b/flash-src/net/gimite/websocket/WebSocketMain.as
index 10fb3af..891a5d1 100644
--- a/flash-src/WebSocketMain.as
+++ b/flash-src/net/gimite/websocket/WebSocketMain.as
@@ -3,7 +3,7 @@
// Reference: http://dev.w3.org/html5/websockets/
// Reference: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07
-package {
+package net.gimite.websocket {
import flash.display.Sprite;
import flash.external.ExternalInterface;
diff --git a/flash-src/WebSocketMainInsecure.as b/flash-src/net/gimite/websocket/WebSocketMainInsecure.as
index 1c5e8d5..eb4f426 100644
--- a/flash-src/WebSocketMainInsecure.as
+++ b/flash-src/net/gimite/websocket/WebSocketMainInsecure.as
@@ -3,9 +3,9 @@
// Reference: http://dev.w3.org/html5/websockets/
// Reference: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07
-package {
+package net.gimite.websocket {
-import flash.system.*;
+import flash.system.Security;
public class WebSocketMainInsecure extends WebSocketMain {
diff --git a/web_socket.js b/web_socket.js
index 1cf0249..8503cc2 100644
--- a/web_socket.js
+++ b/web_socket.js
@@ -5,19 +5,25 @@
(function() {
- if (window.WebSocket) return;
-
- var console = window.console;
- if (!console || !console.log || !console.error) {
- console = {log: function(){ }, error: function(){ }};
+ if (window.WebSocket && !window.WEB_SOCKET_FORCE_FLASH) return;
+
+ var logger;
+ if (window.WEB_SOCKET_LOGGER) {
+ logger = WEB_SOCKET_LOGGER;
+ } else if (window.console && window.console.log && window.console.error) {
+ // In some environment, console is defined but console.log or console.error is missing.
+ logger = window.console;
+ } else {
+ logger = {log: function(){ }, error: function(){ }};
}
- if (!swfobject.hasFlashPlayerVersion("10.0.0")) {
- console.error("Flash Player >= 10.0.0 is required.");
+ // swfobject.hasFlashPlayerVersion("10.0.0") doesn't work with Gnash.
+ if (swfobject.getFlashPlayerVersion().major < 10) {
+ logger.error("Flash Player >= 10.0.0 is required.");
return;
}
if (location.protocol == "file:") {
- console.error(
+ logger.error(
"WARNING: web-socket-js doesn't work in file:///... URL " +
"unless you set Flash Security Settings properly. " +
"Open the page via Web server i.e. http://...");
@@ -222,9 +228,22 @@
window.WEB_SOCKET_SWF_LOCATION = WebSocket.__swfLocation;
}
if (!window.WEB_SOCKET_SWF_LOCATION) {
- console.error("[WebSocket] set WEB_SOCKET_SWF_LOCATION to location of WebSocketMain.swf");
+ logger.error("[WebSocket] set WEB_SOCKET_SWF_LOCATION to location of WebSocketMain.swf");
return;
}
+ if (!window.WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR &&
+ !WEB_SOCKET_SWF_LOCATION.match(/(^|\/)WebSocketMainInsecure\.swf(\?.*)?$/) &&
+ WEB_SOCKET_SWF_LOCATION.match(/^\w+:\/\/([^\/]+)/)) {
+ var swfHost = RegExp.$1;
+ if (location.host != swfHost) {
+ logger.error(
+ "[WebSocket] You must host HTML and WebSocketMain.swf in the same host " +
+ "('" + location.host + "' != '" + swfHost + "'). " +
+ "See also 'How to host HTML file and SWF file in different domains' section " +
+ "in README.md. If you use WebSocketMainInsecure.swf, you can suppress this message " +
+ "by WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;");
+ }
+ }
var container = document.createElement("div");
container.id = "webSocketContainer";
// Hides Flash box. We cannot use display: none or visibility: hidden because it prevents
@@ -258,7 +277,7 @@
null,
function(e) {
if (!e.success) {
- console.error("[WebSocket] swfobject.embedSWF failed");
+ logger.error("[WebSocket] swfobject.embedSWF failed");
}
});
};
@@ -295,7 +314,7 @@
WebSocket.__instances[events[i].webSocketId].__handleEvent(events[i]);
}
} catch (e) {
- console.error(e);
+ logger.error(e);
}
}, 0);
return true;
@@ -303,12 +322,12 @@
// Called by Flash.
WebSocket.__log = function(message) {
- console.log(decodeURIComponent(message));
+ logger.log(decodeURIComponent(message));
};
// Called by Flash.
WebSocket.__error = function(message) {
- console.error(decodeURIComponent(message));
+ logger.error(decodeURIComponent(message));
};
WebSocket.__addTask = function(task) {