微信发送给服务端是数据能正常接收到,但是服务端发送给微信端的接收不到,服务端和网页的websocket交互正常,但是和微信这就不行了
微信代码如下
var
urls = require(
'../constant.js'
).urls;
var
Stomp = require(
'../lib/stomp.js'
).Stomp;
function
request(url, data, success) {
wx.getStorage({
key:
'net_sessionid'
,
success:
function
(sessionIdRes) {
console.log(
'sessionID:'
+ sessionIdRes.data);
wx.request({
url: url,
data: data,
method:
'POST'
,
dataType:
'json'
,
header: {
'content-type'
:
'application/x-www-form-urlencoded'
,
// 默认值
'Cookie'
:
'JSESSIONID='
+ sessionIdRes.data
},
success:
function
(res) {
success(res);
}
})
}
})
}
function
sendSocketMessage(msg) {
console.log(
'send msg:'
)
console.log(msg);
wx.sendSocketMessage({
data: msg,
success:
function
(res) {
console.log(
'消息发送成功'
);
wx.setStorage({
key:
"msg_"
,
data: msg
})
}
})
}
function
onSocketOpen() {
}
var
ws = {
send: sendSocketMessage,
onopen:
null
,
onmessage:
null
}
var
stompClient = Stomp.over(ws);
function
testSocet() {
wx.connectSocket({
url:
'wss://192.168.1.85:8443/websocket'
,
//url: 'ws://127.0.0.1:8080/websocket/websocket',
});
wx.onSocketMessage(
function
(res) {
console.log(
'收到服务器内容:'
);
console.log(
'收到服务器内容:'
+ res.data)
});
wx.onSocketClose(
function
(res) {
console.log(
'WebSocket 已关闭!'
)
})
wx.onSocketOpen(
function
(res) {
console.log(
'WebSocket连接已打开!'
)
stompClient.send(
"/app/req"
, {},
"ssss"
);
})
Stomp.setInterval =
function
(interval, f) {
return
setInterval(f, interval);
};
Stomp.clearInterval =
function
(id) {
return
clearInterval(id);
};
wx.onSocketError(
function
(res) {
console.log(
'WebSocket连接打开失败,请检查!'
)
})
}
// stompClient.connect({}
// // ,
// // function connectCallback(frame) {
// // console.log('已连接');
// // stompClient.subscribe('/topic/resp', function (response) {
// // console.log("返回内容:" + response.body);
// // });
// // },
// // function errorCallBack(error) {
// // console.log('连接失败【' + error + '】');
// // }
// );
function
init() {
//getSession();
testSocet();
}
function
getSession() {
wx.request({
url: urls.getSession,
method:
'POST'
,
dataType:
'json'
,
header: {
'content-type'
:
'application/x-www-form-urlencoded'
,
// 默认值
},
success:
function
(res) {
wx.setStorage({
key:
"net_sessionid"
,
data: res.data.data
})
}
})
}
module.exports = {
request: request,
init: init
}
|
结果
服务端代码
package
com.hykj.zonechat.controller;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.messaging.handler.annotation.MessageMapping;
import
org.springframework.messaging.simp.SimpMessagingTemplate;
import
org.springframework.messaging.simp.annotation.SendToUser;
import
org.springframework.stereotype.Controller;
@Controller
public
class
SocketController {
private
final
Logger log = LoggerFactory.getLogger(
this
.getClass());
@Autowired
private
SimpMessagingTemplate simpMessagingTemplate;
@MessageMapping
(
"/req"
)
public
void
sendPublicMessage(String msg) {
log.info(
"收到消息:"
+msg);
simpMessagingTemplate.convertAndSend(
"/topic/resp"
,
"qwerty"
);
}
}
|
package
com.hykj.zonechat.sys;
import
org.springframework.context.annotation.Configuration;
import
org.springframework.messaging.simp.config.MessageBrokerRegistry;
import
org.springframework.web.socket.config.annotation.*;
@Configuration
@EnableWebSocketMessageBroker
public
class
WebSocketConfig
implements
WebSocketMessageBrokerConfigurer {
@Override
public
void
registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint(
"/websocket"
).setAllowedOrigins(
"*"
);
}
@Override
public
void
configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker(
"/topic/"
);
config.setApplicationDestinationPrefixes(
"/app"
);
}
@Override
public
void
configureWebSocketTransport(WebSocketTransportRegistration registration) {
registration.setMessageSizeLimit(
128
*
1024
);
}
}
|
结果
网页代码
|
结果
这个问题,弄了好久都没搞定,后台用的是springboot 2.0.1.RELEASE 版本 是希望借助websocket 和 stomp协议进行交互,但是在微信和服务端进行websocket交互时出现问题了,就是微信发给后台的数据,后台能够接收到,但是后台发送给微信小程序的数据,微信端却接收不到,期间用网页进行测试,发现网页能够接收到数据,然后又用最基础的servlet建立一个简单的websocket进行测试,发现微信端还是收不到数据,
希望各位前辈能给予解惑,谢谢,联系方式qq 1324618209