//开启websocket
openSocket:function(){
//申请
wx.connectSocket({
url: ‘http://localhost:8080/chanxiu_v04/chat’,
data:‘need help’,
header: {
‘content-type’: ‘application/json’
},
success:function(res){
console.log(“调用wx.connectSocket成功”);
},
fail:function(res){
console.log(“调用wx.connectSocket失败:\n”+res.data);
}
});
//成功连接
wx.onSocketOpen(function(res){
console.log(“调用wx.onSocketOpen成功”);
});
//链接失败
wx.onSocketError(function (res) {
console.log(‘WebSocket连接打开失败,请检查!’)
})
},
//发送消息
sendMessage:function(){
wx.sendSocketMessage({
data: ‘i need money,can you give me some?’
})
},
//关闭连接
closeSocket:function(){
wx.closeSocket({
code: 0,
reason: ‘thanks’,
success: function (res) { },
fail: function (res) { },
complete: function (res) { },
});
//监听是否关闭
wx.onSocketClose(function (res) {
console.log(‘WebSocket 已关闭!’)
})
}
})
点击哪一个function,都不报任何 log 信息和其他信息?请教大神
大神们啊,
你们看这个网上的分享代码:我的实现和他一样的呀
http://blog.csdn.net/manyouzhijian/article/details/54425456
可是我的就是 404 异常,求解!
本地机子测试代码如下:
wx.connectSocket({
url: 'ws://localhost:8080/chanxiu_v04/WebSocket',
data:{},
header: {
'content-type': 'application/json'
},
success:function(res){
console.log("调用wx.connectSocket成功");
},
fail:function(res){
console.log("调用wx.connectSocket失败:\n"+res.data);
}
})
tomcat 正常运行的Java 后端项目chanxiu_v04如下:
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
[@ServerEndpoint](/user/ServerEndpoint)("/WebSocket")
public class MyWebSocket {...........}
一直报以下错误:
asdebug.js:1 WebSocket connection to 'ws://localhost:8080/chanxiu_v04/WebSocket' failed: Error during WebSocket handshake: Unexpected response code: 404