WebSocket 可以连接到本地服务器,但无法发送或接收
WebSocket can connect to local server, but can't send or receive
我正在努力在两个本地服务器之间设置一个 websocket 以进行开发。
一方面,我在 http://localhost:8100/
上安装了我的 Ionic 应用程序 运行
在另一端,我在 http://localhost:9080/ (or http://127.0.0.1:9080)
上有一个 Spring 后端 运行
连接已经建立,所以接下来我想用令牌向 websocket 发送消息(我知道这可以在 SockJS 1.1.0 中建立连接时发送,但我目前正在使用0.3.4)
但是我后台的代码好像没有响应,我想知道我的IP配置是否正确。我按照教程进行操作,并在另一个项目中使用了它。
有经验的人知道订阅函数中的 url 是否也需要加上前缀 'localhost' 或 IP 地址吗?我知道 websocket 从 http:// 更改为 ws:// 所以我想在这种情况下我需要在它前面加上类似的前缀:ws://localhost:9080/...
无论如何,这是我的代码:
WebSocet 服务:
function init() {
var socket = new SockJS('http://127.0.0.1:9080/ws-notification');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log('Connected: ' + frame);
/**
* Subscribe at /ws-topic/greetings url to catch messages
*/
stompClient.subscribe('/ws-topic/greetings', function(greeting){
notify(JSON.parse(greeting.body).content);
});
parseAuthentication();
});
}
function parseAuthentication(){
stompClient.send("/ws-app/ws-notification",{},JSON.stringify({ 'token': authenticationService.isAuthenticated() }));
}
function disconnect() {
if (stompClient != null) {
stompClient.disconnect();
}
// setConnected(false);
console.log("Disconnected");
}
function notify(message){
console.log("NOTIFY: "+message);
}
WebSocket 配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws-notification").setAllowedOrigins("*").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config){
config.enableSimpleBroker("/ws-topic");
config.setApplicationDestinationPrefixes("/ws-app");
}
}
我的控制器功能:
@MessageMapping("/ws-notification")
@SendTo("/ws-topic/greetings")
public Notify greeting(Notify notify) throws InterruptedException {
Thread.sleep(1000);
return new Notify("Hello, your token is :" + notify.getWsToken());
}
请注意,当我在 init() 函数中设置连接时,我只指定了 IP 地址,并尝试在其他 url 的前面加上 ws://127.0.0.1:...但运气不好!
我找到答案了!
问题是我用来发送数据的模型中没有默认的构造方法。
中实施或提及
我正在努力在两个本地服务器之间设置一个 websocket 以进行开发。
一方面,我在 http://localhost:8100/
上安装了我的 Ionic 应用程序 运行在另一端,我在 http://localhost:9080/ (or http://127.0.0.1:9080)
上有一个 Spring 后端 运行连接已经建立,所以接下来我想用令牌向 websocket 发送消息(我知道这可以在 SockJS 1.1.0 中建立连接时发送,但我目前正在使用0.3.4)
但是我后台的代码好像没有响应,我想知道我的IP配置是否正确。我按照教程进行操作,并在另一个项目中使用了它。
有经验的人知道订阅函数中的 url 是否也需要加上前缀 'localhost' 或 IP 地址吗?我知道 websocket 从 http:// 更改为 ws:// 所以我想在这种情况下我需要在它前面加上类似的前缀:ws://localhost:9080/...
无论如何,这是我的代码:
WebSocet 服务:
function init() {
var socket = new SockJS('http://127.0.0.1:9080/ws-notification');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log('Connected: ' + frame);
/**
* Subscribe at /ws-topic/greetings url to catch messages
*/
stompClient.subscribe('/ws-topic/greetings', function(greeting){
notify(JSON.parse(greeting.body).content);
});
parseAuthentication();
});
}
function parseAuthentication(){
stompClient.send("/ws-app/ws-notification",{},JSON.stringify({ 'token': authenticationService.isAuthenticated() }));
}
function disconnect() {
if (stompClient != null) {
stompClient.disconnect();
}
// setConnected(false);
console.log("Disconnected");
}
function notify(message){
console.log("NOTIFY: "+message);
}
WebSocket 配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws-notification").setAllowedOrigins("*").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config){
config.enableSimpleBroker("/ws-topic");
config.setApplicationDestinationPrefixes("/ws-app");
}
}
我的控制器功能:
@MessageMapping("/ws-notification")
@SendTo("/ws-topic/greetings")
public Notify greeting(Notify notify) throws InterruptedException {
Thread.sleep(1000);
return new Notify("Hello, your token is :" + notify.getWsToken());
}
请注意,当我在 init() 函数中设置连接时,我只指定了 IP 地址,并尝试在其他 url 的前面加上 ws://127.0.0.1:...但运气不好!
我找到答案了!
问题是我用来发送数据的模型中没有默认的构造方法。
中实施或提及