WebSocket 接口封装方法

来源:互联网 发布:下载手机办公软件 编辑:程序博客网 时间:2024/06/01 17:02



//外部模块直接调用  sendToserver("/zjh/FGFNoum.do?userId="+userId+"&gameId="+gameId)



        var that = this;
        function sendToServer(protocal){
            var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                        that.receiveFromServer(xhr.responseText)
                    }
                };  
                xhr.open("post", globle.MAIN_URL + ":8080" + protocal);
                xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                xhr.send();
        }


        var msgBack = {
            login:"canvas/suibian/script1",
            logout:"canvas/suibian/script2",
        }


        function receiveFromServer(responseText){
            //根据 responseText 取得返回的协议号
            cc.find(this.msgBack[协议号]).onReceive(处理好的数据)
        }
0 0
原创粉丝点击