var websocket = null;
if ('WebSocket' in window) {
websocket = new WebSocket("ws://localhost:8080/springws/websocket/socketServer.do");
} else if ('MozWebSocket' in window) {
websocket = new MozWebSocket("ws://localhost:8080/springws/websocket/socketServer.do");
} else {
websocket = new SockJS("http://localhost:8080/springws/sockjs/socketServer.do");
}
websocket.onopen = onOpen;
websocket.onmessage = onMessage;
websocket.onerror = onError;
websocket.onclose = onClose;
一直是监听到error,请问有了解的吗。
电脑端浏览器是正常的,手机浏览器检测有WebSocket
localhost: 8080是本地还回地址, 你的websocket的服务是开在你的pc端的,然后你用手机连接,这个localhost就是指手机,你手机并没有开websocket服务
所以一直是error因为你的服务都木有。
你试着把服务部署到外网 然后连接地址通过外网地址连接
或者你用手机连上电脑开的wifi然后配置代理 然后手机可以访问你的pc端上面,然后访问地址localhost改为你电脑的ip地址
onerror连接发生错误的回调方法,onopen成功建立连接的回调,onclose连接关闭的回调,onmessage接收到消息的回调。
localhost是变量吗?
https://stackoverflow.com/questions/18803971/websocket-onerror-how-to-read-error-description
把localhost换成电脑ip试试