2016-08-03 34 views
0

開発のために2つのローカルサーバー間にwebsocketを設定しています。私はhttp://localhost:9080/(またはhttp://127.0.0.1:9080)上で実行されている春のバックエンドを持っているもう一方の端にhttp://localhost:8100/WebSocketはローカルサーバーに接続できますが、送受信はできません

上で実行されている私のイオンアプリを持っている一端に

接続は私がしたいので、次のアップ、確立されましたトークンを使ってwebsocketにメッセージを送ります(接続がSockJS 1.1.0に設定されているときに送ることができますが、現在は0.3.4を使用していますが、私は現在0.3.4を使用しています)

しかし、バックエンドのコードでは、私のIP構成が正しいかどうか疑問に思っています。私はチュートリアルに続き、別のプロジェクトでこれを動作させました。

subscribe関数のurlに 'localhost'またはIP adrressの接頭辞を付ける必要があるかどうかを知っている人は誰ですか? ws:// localhost:9080/...

とにかく、ここには、私は、WebSocketがhttp://からws://に変わることを知っています。私のコード:

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"); 
    } 
} 

マイControllerfunction:

@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:を付けようとしたときにIPアドレスだけを指定していることに注意してください。

答えて

0

答えが見つかりました!

問題は、データの送信に使用したモデルにデフォルトのコンストラクタメソッドがなかったことです。

これはまた、あなたがexaclyそれを修正する方法を説明してもらえ実装またはSpring WebSocket Tutorial

+0

で言及されていませんでしたか?そのチュートリアルで見ているモデルには、デフォルトコンストラクタがあります。私にとっては、春になるものを送ることは可能ですが、購読したチャンネルには何も戻っていません。 –

+0

@JanWytzeおそらくther Object notify、文字列コンストラクタを持っているので、defaltコンストラクタを宣言するのを忘れた場合、defaltコンストラクタは存在せず、springは失敗します。 – Walfrat

関連する問題