由于项目中需要每个电脑连接一个小票打印机,需往socekt端传送数据,socekt端进行解析然后在打印出来
function websocket(){
if ("WebSocket" in window){//判断浏览器是否支持websocekt
var host = "ws://127.0.0.0:18000"//所要连接的地址Ip和端口
var socket = new WebSocket(host);
try {
socket.onopen = function()//打开socekt
{
socket.send(data);//data是将要发送的数据,发送到socekt端
};
socket.onmessage = function(evt)//连接socekt
{
if(evt.data=="success"){//从socekt中返回过来连接成功的值
alert("连接成功......");
}
};
socket.onclose = function()//关闭socekt连接
{
// alert("Socket关闭");
socket.close();
};
socket.onerror = function(obj)//连接错误
{
if("failure"==obj.data){//从socekt中返回过来错误的值
alert("连接错误......");
}
};
}
catch (ex) {
log(ex);
}
}
else{
alert("该浏览器不支持,请切换到 Internet Explorer 10(0)模式");
}
}
//在title下加入以下代码,便可解决ie浏览器不兼容websocekt问题
<meta http-equiv="X-UA-Compatible" content="IE=10">