//---------------------
//-javascript socket
//-近期邮局项目需求,研究下了下javascript socket。
//-网上很多javascript socket都是借助flash as3.0的 Socket Programming,因此很努力的研究了下
//-flash的p2p技术,结果很让我失望,as 跟 js一样始终无法实现点对点连接,f读写操作文件等。要实现这些功能都是要通过CGI模拟的。
//-于是乎搞了下面这个程序
/**
* javascript socket
* @Author:nathena
* @Email:nathenayang@gmail.com
* @Date:200707
*/
function Socket(ip,port,cb,pp)
{
this.ip = ip;
// callee,caller
this.callback = cb?"function callback(){parent."+cb+"()}":"";
this.pp = pp||"";
this.port = port;
this.socketTarget = "";
var _this = this;
this.socket=function()
{
//create hidden socket
/**
* @return socket object;
var _s = document.createElement("form");
_s.innerHTML="<input type='hidden' value='"+Base(p)+"'>";
_s.action = _this.socketTarget;
_s.method ="post";
_s.target="chat"
document.body.appendChild(_s);
_s.submit();
*/
}
this.setIp=function(ip)
{
if(!ip)
throw new Error("Error ip!");
_this.ip = ip;
}
this.getIp=function()
{
return _this.ip;
}
this.setPort=function(port)
{
if(!port)
throw new Error("Error port!");
_this.port = port;
}
this.getPort = function()
{
return _this.port;
}
this.setPP = function(pp)
{
if(!pr)
_this.pp = "http";
else
this.pp = pp;
}
this.getPP = function()
{
return _this.pp;
}
this.setCallback=function((cb=""))
{
_this.callback = cb;
}
this.getCallback=function()
{
return _this.callback;
}
this.setSocketTarget=function(socket)
{
if(socket)
{
_this.socket = socket;
}
else
{
_this.socket=createSocket();
}
}
this.getSocketTarget=function()
{
return _this.socket;
}
function createSocket()
{
var _s;
if(!_this.ip||!_this.port)
throw new Errot("Create Socket faild");
if(pp)
_s = pp+"://"+ip+":"+port;
else
_s = "http://"+ip+":"+port;
return _s
}
}