Permalink
Browse files

Merge remote branch 'gimite/master' into hybi-08

Conflicts:
	WebSocketMain.swf
	WebSocketMainInsecure.zip
  • Loading branch information...
2 parents fa9e686 + ed0622a commit 4be643b29c1146b085e6688d4850e935a165da09 @kanaka kanaka committed Aug 29, 2011
View
@@ -1 +1,2 @@
test.html
+WebSocket.swc
View
@@ -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.
View
@@ -0,0 +1 @@
+build.properties
@@ -0,0 +1,2 @@
+# Point this to your Flex SDK directory.
+FLEX_HOME=/usr/local/share/flex_sdk_4
View
@@ -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
View
@@ -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>
+
@@ -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;
@@ -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);
}
@@ -1,4 +1,4 @@
-package {
+package net.gimite.websocket {
import flash.events.Event;
@@ -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;
@@ -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 {
View
@@ -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,20 +314,20 @@
WebSocket.__instances[events[i].webSocketId].__handleEvent(events[i]);
}
} catch (e) {
- console.error(e);
+ logger.error(e);
}
}, 0);
return true;
};
// 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) {

0 comments on commit 4be643b

Please sign in to comment.