我正在为开发在两个本地服务器之间设置一个websocket。
一方面,我在http://localhost:8100/上运行了我的Ionic应用程序
另一方面,我在http://localhost:9080/ (或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 Config:
@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()函数中设置连接时,尝试在其他url的前缀为ws://127.0.0.1:.但运气不好!
发布于 2016-08-04 11:00:06
https://stackoverflow.com/questions/38745131
复制相似问题