Spring 使用websocket实时更新角度应用程序数据
我在服务器端用Spring编写了API,还管理了websocket代码,该代码打开socket并持续响应一些数据(例如Spring 使用websocket实时更新角度应用程序数据,spring,angular,angular5,sockjs,Spring,Angular,Angular5,Sockjs,我在服务器端用Spring编写了API,还管理了websocket代码,该代码打开socket并持续响应一些数据(例如/getlikes返回likes的数量) 如何在服务中调用此API,以不断检查更新的值(我不想使用任何时间间隔进行服务调用)?您可以使用并执行类似的操作 import { Component } from '@angular/core'; import * as Stomp from 'stompjs'; import * as SockJS from 'sockjs-clien
/getlikes
返回likes的数量)
如何在服务中调用此API,以不断检查更新的值(我不想使用任何时间间隔进行服务调用)?您可以使用并执行类似的操作
import { Component } from '@angular/core';
import * as Stomp from 'stompjs';
import * as SockJS from 'sockjs-client';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private serverUrl = 'http://localhost:8080/socket'
private title = 'WebSockets chat';
private stompClient;
constructor(){
this.initializeWebSocketConnection();
}
public initializeWebSocketConnection(){
let ws = new SockJS(this.serverUrl);
this.stompClient = Stomp.over(ws);
let that = this;
this.stompClient.connect({}, function(frame) {
that.stompClient.subscribe("/chat", (message) => {
if(message.body) {
$(".chat").append("<div class='message'>"+message.body+"</div>")
console.log(message.body);
}
});
});
}
public sendMessage(message){
this.stompClient.send("/app/send/message" , {}, message);
$('#input').val('');
}
}
从'@angular/core'导入{Component};
从“stompjs”导入*作为Stomp;
从“SockJS客户端”导入*作为SockJS;
@组成部分({
选择器:'应用程序根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
专用服务器URL=http://localhost:8080/socket'
私人头衔='WebSockets聊天';
私人stompClient;
构造函数(){
this.initializeWebSocketConnection();
}
公共初始化WebSocketConnection(){
让ws=newsockjs(this.serverUrl);
this.stompClient=Stomp.over(ws);
让那=这;
this.stompClient.connect({},函数(frame){
that.stompClient.subscribe(“/chat”,(message)=>{
if(message.body){
$(“.chat”).append(“+message.body+”)
console.log(message.body);
}
});
});
}
公共发送消息(消息){
this.stompClient.send(“/app/send/message”,{},message);
$('#input').val('');
}
}
你可以在这个中找到关于这方面的完整教程,你可以使用和做类似的事情
import { Component } from '@angular/core';
import * as Stomp from 'stompjs';
import * as SockJS from 'sockjs-client';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private serverUrl = 'http://localhost:8080/socket'
private title = 'WebSockets chat';
private stompClient;
constructor(){
this.initializeWebSocketConnection();
}
public initializeWebSocketConnection(){
let ws = new SockJS(this.serverUrl);
this.stompClient = Stomp.over(ws);
let that = this;
this.stompClient.connect({}, function(frame) {
that.stompClient.subscribe("/chat", (message) => {
if(message.body) {
$(".chat").append("<div class='message'>"+message.body+"</div>")
console.log(message.body);
}
});
});
}
public sendMessage(message){
this.stompClient.send("/app/send/message" , {}, message);
$('#input').val('');
}
}
从'@angular/core'导入{Component};
从“stompjs”导入*作为Stomp;
从“SockJS客户端”导入*作为SockJS;
@组成部分({
选择器:'应用程序根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
专用服务器URL=http://localhost:8080/socket'
私人头衔='WebSockets聊天';
私人stompClient;
构造函数(){
this.initializeWebSocketConnection();
}
公共初始化WebSocketConnection(){
让ws=newsockjs(this.serverUrl);
this.stompClient=Stomp.over(ws);
让那=这;
this.stompClient.connect({},函数(frame){
that.stompClient.subscribe(“/chat”,(message)=>{
if(message.body){
$(“.chat”).append(“+message.body+”)
console.log(message.body);
}
});
});
}
公共发送消息(消息){
this.stompClient.send(“/app/send/message”,{},message);
$('#input').val('');
}
}
你可以在这个@Bhagvat-Lande中找到关于这个的完整教程
我想你正在寻找这个:
@bhagvatlande
我想你正在寻找这个:
@anirudha hie感谢您的回复,我使用的是spring server(不是node js)我们可以这样做吗?SockJs和stompjs您可以使用吗?谢谢我遵循了SockJs和stompjs的文档。您可以添加一些演示,演示如何使用ui上的api更新结果从服务文件和组件.ts调用api。我更新了答案。点击我共享的链接三次。嗨@anirudha我会看一下,你能告诉我:我需要实时更新我的仪表板值,在他的情况下,我只需要订阅这个回复我数据的频道(/聊天),我这里一团糟,你能帮我吗help@anirudha谢谢回复,我使用的是spring服务器(不是NodeJS)我们可以这样做吗?SockJs和stompjs您可以使用吗?谢谢我遵循了SockJs和stompjs的文档。您可以添加一些演示,演示如何使用ui上的api更新结果从服务文件和组件.ts调用api。我更新了答案。点击我共享的链接三次。hi@anirudha我会看一下,你能告诉我:我需要实时更新我的仪表板值,在他的情况下,我只需要订阅这个回复我数据的频道(/chat),我这里一团糟。一旦客户订阅了websocket端点,你能帮我吗,只要随时从服务器发送消息,所有订阅的客户端都将收到消息。一旦客户端订阅了websocket端点,只要随时从服务器发送消息,所有订阅的客户端都将收到消息。谢谢。我需要在SockJS和Stompjs的帮助下完成这项工作,因为我的服务器在春季,建议使用SockJS和Stompjs谢谢。我需要在SockJS和Stompjs的帮助下完成这项工作,因为我的服务器在春季,建议使用SockJS和Stompjs