本文档部分API缺少示例代码,诚挚的邀请你参与到示例代码的编写中来,你可以在代码中发布2行自定义注释,内容自拟。 有意者可与刺鸟联系(QQ:4041990 Q群:7702175)

API简介

uexSocketMgr对象封装了TCP&UDP socket管理接口API。

接口说明

方法说明
uexSocketMgr.createUDPSocket(inOpCode,inPort)创建一个UDP对象。
uexSocketMgr.cbCreateUDPSocket(opId,dataType,data)uexSocketMgr.createUDPSocket的回调方法。
uexSocketMgr.createTCPSocket(inOpCode)创建一个TCP对象。
uexSocketMgr.cbCreateTCPSocket(opId,dataType,data)uexSocketMgr.createTCPSocket的回调方法。
uexSocketMgr.closeSocket(inOpCode)用于关闭UDP或TCP对象。
uexSocketMgr.onData(inOpCode,inMsg)用于监听接收UDP或TCP socket传输的数据。
uexSocketMgr.setTimeOut(inOpCode,inTimeOut)设置连接超时。
uexSocketMgr.setInetAddressAndPort(inOpCode,inRemoteAddress,inRemotePort)设置对方ip地址及端口号。
uexSocketMgr.cbConnected(opId,dataType,data)uexSocketMgr.setInetAddressAndPort的回调方法。
uexSocketMgr.sendData(inOpCode,inMsg)发送数据。
uexSocketMgr.cbSendData(opId,dataType,data)uexSocketMgr.sendData的回调方法。
uexSocketMgr.onDisconnected(inOpCode)通知TCP Socket连接中断。
createUDPSocket(inOpCode,inPort)
参数名称描述
inOpCode操作id(整型)
inPort指定本地端口号。如果是空,随机创建
cbCreateUDPSocket(opId,dataType,data)
参数名称描述
opId操作ID,在此函数中不起作用,可忽略
dataType返回数据的数据类型为uex.cInt(值为2)
data返回的数据,0表示成功或1表示失败
createTCPSocket(inOpCode)
参数名称描述
inOpCode操作id(整型)
cbCreateTCPSocket(opId,dataType,data)
参数名称描述
opId操作ID,在此函数中不起作用,可忽略
dataType返回数据的数据类型为uex.cInt(值为2)
data返回的数据,0表示成功或1表示失败
closeSocket(inOpCode)
参数名称描述
inOpCode操作id(整型)
onData(inOpCode,inMsg)
参数名称描述
inOpCode操作id(整型)
inMsg接收到数据,格式为’{xxx.xxx.xxx.xxx}msg’,即’{}’内是对方ip,msg表示收到数据内容
setTimeOut(inOpCode,inTimeOut)
参数名称描述
inOpCode操作id(整型)
inTimeOut超时时间(单位:毫秒)
setInetAddressAndPort(inOpCode,inRemoteAddress,inRemotePort)
参数名称描述
inOpCode操作id(整型)
inRemoteAddress对方ip
inRemotePort对方端口号
cbConnected(opId,dataType,data)
参数名称描述
opId操作ID,在此函数中不起作用,可忽略
dataType返回数据的数据类型为uex.cInt(值为2)
data返回的数据,0表示成功或1表示失败
sendData(inOpCode,inMsg)
参数名称描述
inOpCode操作id(整型)
inMsg要发的消息
cbSendData(opId,dataType,data)
参数名称描述
opId操作ID,在此函数中不起作用,可忽略
dataType返回数据的数据类型为uex.cInt(值为2)
data返回的数据,0表示成功或1表示失败
onDisconnected(inOpCode)

参数名称描述
inOpCode操作id(整型)

// 本代码由寒暄提供,如有bug请和寒暄联系(QQ:457696322 Q群:7702175)
<!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>