menu

秋梦无痕

一场秋雨无梦痕,春夜清风冻煞人。冬来冷水寒似铁,夏至京北蟑满城。

Avatar

利用flash实现javascript支持socket

在网上闲逛,发现一个有趣的东西:socketjs
说白了很简单,就是利用flash中对socket的支持实现javascript中对socket的支持。这样做的好处是可以真正的push东西到客户端,坏处是长链接数量受到服务器端的限制。

下面是用法示例:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="1" height="1" id="socket" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="http://dev.dschini.org/socketjs/socket.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="http://dev.dschini.org/socketjs/socket.swf" quality="high" bgcolor="#ffffff" width="1" height="1" name="socket" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />


<SCRIPT LANGUAGE=JavaScript>
<!--
/*
* This is just a help function !!!
*/
var nickname;
function connect_user(){
window.document.socket.SetVariable("data", "PASS xxx");
window.document.socket.TCallLabel("/", "send" );
window.document.socket.SetVariable("data", "NICK "+nickname);
window.document.socket.TCallLabel("/", "send" )
document.getElementById("output").value += "\nNICK "+nickname;
window.document.socket.SetVariable("data", "USER "+nickname+" 0 * : undefined");
window.document.socket.TCallLabel("/", "send" )
document.getElementById("output").value += "\nUSER "+nickname+" 0 * : undefined";
window.document.socket.SetVariable("data", "JOIN #findthebug");
window.document.socket.TCallLabel("/", "send" )
document.getElementById("output").value += "\nJOIN #findthebug";
document.getElementById("output").scrollTop = document.getElementById("output").scrollHeight;
}

/*
* ----------------------------------
* SocketJS Functions
* ----------------------------------
* (c) 2006 by Manfred Weber
* ----------------------------------
*/
/*
* SocketOnInit()
* Event Handler is called when Flash File is loaded
*/
function SocketOnInit(){};
/*
* SocketOnData()
* Event Handler is called when received Data
*/
function SocketOnData(data){
document.getElementById("output").value += "\n"+data;
document.getElementById("output").scrollTop = document.getElementById("output").scrollHeight;
}
/*
* SocketOnConnect(success);
* Event Handler is called when socket is connected
*/
function SocketOnConnect(success){
if(success=="true"){
document.getElementById("output").value += "\n Connection established";
connect_user();
} else{
document.getElementById("output").value += "\n Connection failed";
}
}
/*
* SocketOnClose
* Event Handler is calles when socket is closed
*/
function SocketOnClose(){
document.getElementById("output").value += "\n Connection closed";
}
/*
* SocketClose()
* Close the Socket
*/
function SocketClose(){
window.document.socket.TCallLabel("/", "close" );
}
/*
* SocketConnect(host,port)
* Connect to socket. Notice that host must be the same where the .swf file resides!
*/
function SocketConnect(host,port){
nickname = document.getElementById("nickname").value;
window.document.socket.SetVariable("host", host);
window.document.socket.SetVariable("port", port);
window.document.socket.TCallLabel("/", "connect" );
}
/*
* SocketSend(data)
* Send data to open socket
*/
function SocketSend(data){
window.document.socket.SetVariable("data", data);
window.document.socket.TCallLabel("/", "send" )
}
//-----------------------------------------------
//-->
</SCRIPT>


服务器端的设置:
[crossdomain.xml 的内容]
<cross-domain-policy>
<allow-access-from domain="*" to-ports="*" />
</cross-domain-policy>

[socket.swf 中必须的AS代码]
System.security.loadPolicyFile("http://dev.dschini.org/crossdomain.xml");

这样,客户端通过SocketConnect('dev.dschini.org',6667); 连接就可以了。

另一个比较类似的,国人写的,http://www.xsock.net/

Flash 真是个罪恶的东西……

哈哈,我喜欢看到桌面化的web程序。

天啊,MS无所不能啊,那Flash可以调用Shell么?

flash在网页上的安全范围内,是不能调用shell的。
在个人机器上好像可以,但是意义不大。

评论已关闭