tomcat - spring boot rest and angular2 with websocket (stomp over sockjs) -


is possible use stomp on sockjs without mvc. have spring rest interface in tomcat, , angular2 application run express.

websocketconfig.java

@configuration @enablewebsocketmessagebroker public class websocketconfig extends abstractwebsocketmessagebrokerconfigurer {      @override     public void registerstompendpoints(stompendpointregistry registry) {         // endpoint websocket connections         registry.addendpoint("/portfolio").setallowedorigins("*").withsockjs();     }      @override     public void configuremessagebroker(messagebrokerregistry config) {         config.setapplicationdestinationprefixes("/app");         config.enablesimplebroker("/topic");     } } 

socketcontroller.java

@controller public class socketcontroller {      @autowired     private simpmessagingtemplate template;      public socketcontroller() {         int = 5;     }      @messagemapping("/greeting")     public string handle(string greeting) {         return "[" + "greeting" + ": " + greeting;     } } 

and typescript code:

. . .

constructor() {          var socket = new sockjs('http://localhost:8080/portfolio');         this.stompclient = stomp.over(socket);         this.stompclient.connect("guest", "guest", function(frame) {             console.log('connected: ' + frame);             this.stompclient.subscribe('http://localhost:8080/topic/greeting', function(greeting) {                 console.log("from from", greeting);             });         }, function (err) {             console.log('err', err);         });     } 

. . .

send() {     this.stompclient.send("http://localhost:8080/app/greeting", {}, json.stringify({ 'name': "kitica" })); } 

. . .

but reason not working.. in console output:

opening web socket... stomp.js:134 web socket opened... stomp.js:134 >>> connect login:guest passcode:guest accept-version:1.1,1.0 heart-beat:10000,10000    stomp.js:134 <<< connected version:1.1 heart-beat:0,0    stomp.js:134 connected server undefined activity-socket.ts:17 connected: connected heart-beat:0,0 version:1.1 

and when send get

>>> send destination:http://localhost:8080/app/greeting content-length:17 

{"name":"kitica"}

but message never comes subscriber.

angular2 on port 8001 , spring rest on 8080

the part confusing using spring-boot-rest , not serving angular2 static tomcat container, have angular2 under webpack trying subscribe , send relative url.

the right way is:

import {component} '@angular/core'; import {activityservice} '../common/services'; import {materializedirective} 'angular2-materialize'; import {larsactionbuttoncomponent} '../common/components';  var sockjs = require('sockjs-client'); var stomp = require('stompjs');  @component({ selector: 'activity', providers: [activityservice], directives: [materializedirective, larsactionbuttoncomponent], templateurl: 'app/activity/activity.html' })  export class activity { stompclient: any;  activityid: any; text: any; messages: array<string> = new array<string>();  constructor() { }  send() {     this.stompclient.send('/app/hello/' + this.activityid, {},      json.stringify({ 'name': this.text })); }  connect() {     var = this;     var socket = new sockjs('tst-rest.mypageexample/hello?activityid=' + this.activityid);     this.stompclient = stomp.over(socket);     this.stompclient.connect({}, function (frame) {         console.log('connected: ' + frame);         that.stompclient.subscribe('/topic/greetings/' + that.activityid, function (greeting) {             that.messages.push(json.parse(greeting.body).content);         });     }, function (err) {         console.log('err', err);     }); }  } 

and in spring controller:

@controller public class socketcontroller {   @messagemapping("/hello") @sendto("/topic/greetings") public greeting greeting(hellomessage message) throws exception {     return new greeting("hello, " + message.getname() + "!"); }  } 

configuration class:

@configuration @enablewebsocketmessagebroker public class websocketconfig extends abstractwebsocketmessagebrokerconfigurer {      @override     public void configuremessagebroker(messagebrokerregistry config) {         config.enablesimplebroker("/topic");         config.setapplicationdestinationprefixes("/app");     }      @override     public void registerstompendpoints(stompendpointregistry registry) {         registry.addendpoint("/hello").setallowedorigins("*").withsockjs();     }  } 

Comments