当前位置:   article > 正文

springboot+websocket+angular_springboot xterm websocket angularjs

springboot xterm websocket angularjs

一、websocket服务端代码参考 另一篇博客 --> 传送门

二、ng new socket 创建angular demo项目

ng new socket

三、安装 stompjs js库

npm install stompjs --save

四、app.component.ts 代码 (和js的写法大同小异)

  1. import { Component } from '@angular/core';
  2. const SockJS = require('sockjs-client');
  3. const Stomp = require('stompjs');
  4. @Component({
  5. selector: 'app-root',
  6. templateUrl: './app.component.html',
  7. styleUrls: ['./app.component.css']
  8. })
  9. export class AppComponent {
  10. title = 'app works!';
  11. stompClient: any;
  12. constructor() {
  13. const that = this;
  14. const socket = new SockJS('http://localhost:8080/websocket-server');
  15. this.stompClient = Stomp.over(socket);
  16. this.stompClient.connect({}, function (frame) {
  17. console.log('Connected: ' + frame);
  18. that.stompClient.subscribe('/topic/message', function (greeting) {
  19. alert(greeting);
  20. });
  21. }, function (err) {
  22. console.log('err', err);
  23. });
  24. }
  25. send() {
  26. this.stompClient.send('/server/send', {}, 'send');
  27. }
  28. }

五、app.component.html

<div><button (click)="send()">点击</button></div>

六、启动java项目和angular项目 访问localhost:4200 点击按钮测试

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/588475
推荐阅读
相关标签
  

闽ICP备14008679号