uexSocketMgr对象封装了TCP&UDP socket管理接口API。
使用示例
以下示例代码说明了如何使用uexSocketMgr对象的方法.
<!DOCTYPE html>
<html>
<head>
<title>AppCan API uexSocketMgr</title>
<meta charset='utf-8'>
<script>
var socketUDP = 1;
var socketTCP = 2;
function creatUDP(port){
uexSocketMgr.createUDPSocket(socketUDP,port);
}
function setUDPIPandPort(ip,port){
uexSocketMgr.setInetAddressAndPort(socketUDP,ip,port);
}
function sendUDP(data){
uexSocketMgr.sendData(socketUDP,data);
}
function creatTCP(){
tcp = uexSocketMgr.createTCPSocket(socketTCP);
//tcp.setInetAddressAndPort('192.168.1.28',2009);
}
function setTCPIPandPort(ip,port){
uexSocketMgr.setInetAddressAndPort(socketTCP,ip,port);
}
function sendTCP(data){
uexSocketMgr.sendData(socketTCP,data);
}
function closeUDP(){
uexSocketMgr.closeSocket(socketUDP);
}
function closeTCP(){
uexSocketMgr.closeSocket(socketTCP);
}
window.uexOnload = function(){
uexWidgetOne.cbError = function(opCode,errorCode,errorInfo){
console.log(errorInfo);
}
uexSocketMgr.onData = function(opCode,text){
if(opCode == socketUDP){
document.getElementById('getUDPData').innerHTML = text;
}
if(opCode == socketTCP){
document.getElementById('getTCPData').innerHTML =text;
}
}
var cText = 0;
var cJson = 1;
var cInt = 2;
uexSocketMgr.cbCreateTCPSocket = function(opCode,dataType,data){
switch(dataType){
case cText:
console.log('uex.cText');
break;
case cJson:
console.log('uex.cText');
break;
case cInt:
if(data == 0){
console.log('创建TCP成功');
}else{
console.log('创建TCP失败');
}
break;
default:
console.log('error');
}
}
uexSocketMgr.cbCreateUDPSocket = function(opCode,dataType,data){
switch(dataType){
case cText:
console.log('uex.cText');
break;
case cJson:
console.log('uex.cText');
break;
case cInt:
if(data == 0){
console.log('创建UDP成功');
}else{
console.log('创建UDP失败');
}
break;
default:
console.log('error');
}
}
uexSocketMgr.cbSendData = function(opCode,dataType,data){
switch(dataType){
case cText:
console.log('uex.cText');
break;
case cJson:
console.log('uex.cText');
break;
case cInt:
if(data == 0){
console.log('发送成功');
}else{
console.log('发送失败');
}
break;
default:
console.log('error');
}
}
}
</script>
</head>
<body>
<div class='tit'>socket功能</div>
<div class='conbor'>
<div class='consj'>
<span>1.UDP功能测试:</span>
<span>绑定本地的UDP端口号:</span>
<input class='textbox' type='text' id = 'localPort' value='45666' >
<input class='btn' type='button' value='创建UDP' onclick='creatUDP(document.getElementById('localPort').value);'>
<span>对方的ip:</span>
<input class='textbox' type='text' id = 'udpId' value='192.168.1.28' >
<span>对方的port:</span>
<input class='textbox' type='text' id = 'udpPort' value='22222'>
<input class='btn' type='button' value='设置ip和port' onclick='setUDPIPandPort(document.getElementById('udpId').value,document.getElementById('udpPort').value);'>
<span>输入要发送的UDP数据:</span>
<textarea class='tcxx' id='udpData'>发送给UDP服务器的数据</textarea>
<input class='btn' type='button' value='发送UDP' onclick='sendUDP(document.getElementById('udpData').value);'>
<span>接收到的UDP数据为:</span>
<div class='tcxx' id='getUDPData'></div>
<input class='btn' type='button' value='关闭UDP' onclick='closeUDP();'>
<span>2.TCP功能测试:</span>
<input class='btn' type='button' value='创建TCP' onclick='creatTCP();'>
<span>对方的ip:</span>
<input class='textbox' type='text' id = 'tcpId' value='192.168.1.28' >
<span>对方的port:</span>
<input class='textbox' type='text' id = 'tcpPort' value='2009'>
<input class='btn' type='button' value='设置ip和port' onclick='setTCPIPandPort(document.getElementById('tcpId').value,document.getElementById('tcpPort').value);'>
<span>输入要发送的TCP数据:</span>
<textarea class='tcxx' id='tcpData'>发送给TCP服务器的数据</textarea>
<input class='btn' type='button' value='发送TCP' onclick='sendTCP(document.getElementById('tcpData').value)'>
<span>接收到的TCP数据为:</span>
<div class='tcxx' id='getTCPData'></div>
<input class='btn' type='button' value='关闭TCP' onclick='closeTCP();'>
</div>
</div>
</body>
</html>
使用示例
以下示例代码说明了如何使用uexSocketMgr对象的方法.
<!DOCTYPE html>
<html>
<head>
<title>AppCan API uexSocketMgr</title>
<meta charset='utf-8'>
<script>
var socketUDP = 1;
var socketTCP = 2;
function creatUDP(port){
uexSocketMgr.createUDPSocket(socketUDP,port);
}
function setUDPIPandPort(ip,port){
uexSocketMgr.setInetAddressAndPort(socketUDP,ip,port);
}
function sendUDP(data){
uexSocketMgr.sendData(socketUDP,data);
}
function creatTCP(){
tcp = uexSocketMgr.createTCPSocket(socketTCP);
//tcp.setInetAddressAndPort('192.168.1.28',2009);
}
function setTCPIPandPort(ip,port){
uexSocketMgr.setInetAddressAndPort(socketTCP,ip,port);
}
function sendTCP(data){
uexSocketMgr.sendData(socketTCP,data);
}
function closeUDP(){
uexSocketMgr.closeSocket(socketUDP);
}
function closeTCP(){
uexSocketMgr.closeSocket(socketTCP);
}
window.uexOnload = function(){
uexWidgetOne.cbError = function(opCode,errorCode,errorInfo){
console.log(errorInfo);
}
uexSocketMgr.onData = function(opCode,text){
if(opCode == socketUDP){
document.getElementById('getUDPData').innerHTML = text;
}
if(opCode == socketTCP){
document.getElementById('getTCPData').innerHTML =text;
}
}
var cText = 0;
var cJson = 1;
var cInt = 2;
uexSocketMgr.cbCreateTCPSocket = function(opCode,dataType,data){
switch(dataType){
case cText:
console.log('uex.cText');
break;
case cJson:
console.log('uex.cText');
break;
case cInt:
if(data == 0){
console.log('创建TCP成功');
}else{
console.log('创建TCP失败');
}
break;
default:
console.log('error');
}
}
uexSocketMgr.cbCreateUDPSocket = function(opCode,dataType,data){
switch(dataType){
case cText:
console.log('uex.cText');
break;
case cJson:
console.log('uex.cText');
break;
case cInt:
if(data == 0){
console.log('创建UDP成功');
}else{
console.log('创建UDP失败');
}
break;
default:
console.log('error');
}
}
uexSocketMgr.cbSendData = function(opCode,dataType,data){
switch(dataType){
case cText:
console.log('uex.cText');
break;
case cJson:
console.log('uex.cText');
break;
case cInt:
if(data == 0){
console.log('发送成功');
}else{
console.log('发送失败');
}
break;
default:
console.log('error');
}
}
}
</script>
</head>
<body>
<div class='tit'>socket功能</div>
<div class='conbor'>
<div class='consj'>
<span>1.UDP功能测试:</span>
<span>绑定本地的UDP端口号:</span>
<input class='textbox' type='text' id = 'localPort' value='45666' >
<input class='btn' type='button' value='创建UDP' onclick='creatUDP(document.getElementById('localPort').value);'>
<span>对方的ip:</span>
<input class='textbox' type='text' id = 'udpId' value='192.168.1.28' >
<span>对方的port:</span>
<input class='textbox' type='text' id = 'udpPort' value='22222'>
<input class='btn' type='button' value='设置ip和port' onclick='setUDPIPandPort(document.getElementById('udpId').value,document.getElementById('udpPort').value);'>
<span>输入要发送的UDP数据:</span>
<textarea class='tcxx' id='udpData'>发送给UDP服务器的数据</textarea>
<input class='btn' type='button' value='发送UDP' onclick='sendUDP(document.getElementById('udpData').value);'>
<span>接收到的UDP数据为:</span>
<div class='tcxx' id='getUDPData'></div>
<input class='btn' type='button' value='关闭UDP' onclick='closeUDP();'>
<span>2.TCP功能测试:</span>
<input class='btn' type='button' value='创建TCP' onclick='creatTCP();'>
<span>对方的ip:</span>
<input class='textbox' type='text' id = 'tcpId' value='192.168.1.28' >
<span>对方的port:</span>
<input class='textbox' type='text' id = 'tcpPort' value='2009'>
<input class='btn' type='button' value='设置ip和port' onclick='setTCPIPandPort(document.getElementById('tcpId').value,document.getElementById('tcpPort').value);'>
<span>输入要发送的TCP数据:</span>
<textarea class='tcxx' id='tcpData'>发送给TCP服务器的数据</textarea>
<input class='btn' type='button' value='发送TCP' onclick='sendTCP(document.getElementById('tcpData').value)'>
<span>接收到的TCP数据为:</span>
<div class='tcxx' id='getTCPData'></div>
<input class='btn' type='button' value='关闭TCP' onclick='closeTCP();'>
</div>
</div>
</body>
</html>