订阅一个具有角度的websocket的连续数据
Subscribe to a continuous data with angular-websocket
我正在做一个Angular应用程序,用于显示持续发送给spring服务器的数据。我的Angular应用程序连接正确,但我不知道如何订阅服务器发送的数据。我给你看我的代码:
服务器,WebSocketConfiguration:
...
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(final StompEndpointRegistry registry){
registry.addEndpoint("/random")
.setAllowedOrigins("*")
.withSockJS();
}
....
服务器,数据生成器:
...
@SuppressWarnings("deprecation")
@Component
public class JapcDataGenerator implements ApplicationListener<BrokerAvailabilityEvent> {
[...]
public void sendDataUpdates(int val) {
System.out.println("sending : " + val);
this.messagingTemplate.convertAndSend("/data", val);
}
...
客户端,index.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>WebSocket chart example</title>
</head>
<body ng-app="WebSocketAngularApp">
<div ng-controller="socktCtrl">
<p>{{data}}</p>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script type="text/javascript" src="node_modules/angular-websocket/angular-websocket.js"></script>
<script type="text/javascript" src="resources/application.js" ></script>
</body>
</html>
客户端应用程序.js:
var socktApp = angular.module('WebSocketAngularApp',['ngWebSocket']);
socktApp.factory('mySockt',function($websocket){
var ws = $websocket('ws://137.138.245.169:8888/serverEx/random/websocket');
var collection = [];
ws.onMessage( function(message){
console.info("message: ", message);
collection.push(JSON.parse(message.data));
});
ws.onOpen( function(message){
console.log("Connection open!",message);
});
ws.onClose( function(){
console.log("Closing the socket.")
});
ws.onError( function(message){
console.info("Error in socket", message);
});
var methods = {
collection: collection,
get: function(){
ws.send( JSON.stringify({action: 'data'}) );
}
};
return methods;
});
socktApp.controller ('socktCtrl', function($scope, mySockt){
$scope.data = mySockt;
});
结果显示了连接日志,但没有显示消息,因为我不知道如何订阅此数据流(行:this.messagingTemplate.convertAndSend("/data", val);
)。
尝试使用ws.on('message', function...
message
是从服务器发出的事件名称。
相关文章:
- 订阅一个具有角度的websocket的连续数据
- websocket客户端在发送大数据时断开连接
- 如何从websocket url获取数据
- 使用牛仔和MessagePack通过websocket发送二进制数据
- 在Angular 2中与多个组件共享websocket数据
- 从 websocket 接收数据
- 如何通过 websocket 高效处理大量 HTML5 画布像素数据
- Websocket 不会向所有客户端发送数据
- Websocket - 客户端不接收数据
- Websocket 无法发送大型数据
- 如何解析数据和二进制Websocket消息
- 节点.js,Websocket 服务器端客户端数据
- 当源是 base64 websocket 数据时,图像加载不触发 JavaScript
- 对通过 WebSocket 发送的数据进行速率限制
- 是否有可能发送超过126个字符的websocket数据
- WebSocket数据压缩
- parseInt返回WebSocket数据的奇怪值
- 节点服务器因大量websocket数据传输而冻结
- Angularjs模型在服务器推送websocket数据后发生了变化
- yEnc 用于二进制 websocket 数据