[英]Using socket.io to send data from Sails.js server to Angular2 client side
I'm trying to emit a message from my server side Sails application to the front end Angular2. 我正在尝试从服务器端Sails应用程序向前端Angular2发出消息。 i want both client and server side to be in the same app, so after some searching I found this repo . 我希望客户端和服务器端都在同一个应用程序中,所以经过一番搜索后,我找到了这个仓库 。 I had to import socket.io in typescript with the help of another stack overflow post and added @types/socket.io-client
and socket.io-client
in my package json. 我必须借助另一个堆栈溢出帖子将Type.soso.io导入socket.io,并在包json中添加了@types/socket.io-client
和socket.io-client
。 I will include all of my files. 我将包括所有文件。
This is my initial vies/layout.ejs file 这是我最初的vies / layout.ejs文件
<!DOCTYPE html>
<html>
<head>
<title><%=typeof title == 'undefined' ? 'New Sails App' : title%></title>
<!-- Viewport mobile tag for sensible mobile support -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!--STYLES-->
<link rel="stylesheet" href="/styles/importer.css">
<!--STYLES END-->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
</head>
<body>
<%- body %>
<!--SCRIPTS-->
<script src="/js/dependencies/sails.io.js"></script>
<script src="/js/systemjs.config.js"></script>
<!--SCRIPTS END-->
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</body>
</html>
in my system.js.config.js file I've added "socket.io-client": 'node_modules/socket.io-client/dist/socket.io.js'
to my map and 'socket.io-client': {defaultExtension: 'js'}
to packages. 在我的system.js.config.js文件中,我向地图和'socket.io-client': {defaultExtension: 'js'}
添加了"socket.io-client": 'node_modules/socket.io-client/dist/socket.io.js'
'socket.io-client': {defaultExtension: 'js'}
打包。
In my component I include it with import * as io from "socket.io-client";
在我的组件中,我将import * as io from "socket.io-client";
包含在内import * as io from "socket.io-client";
and after that I've 然后我
export class AppComponent implements OnInit {
constructor(){}
ngOnInit() {
let socket = io();
socket.on('message', function (data){
console.log(data.greeting);
}.bind(this));
}
}
My back end is a simple controler 我的后端是一个简单的控制器
emit: function (req, res) {
console.log('sending message');
sails.sockets.broadcast('message', { greeting: 'Hola!' });
return res.json(200, {success: true});
}
When I call the emit
action I get sending message
, but nothing logs in the front end. 当我调用emit
动作时,我会sending message
,但是前端没有任何记录。 There are no errors anywhere, but nothing happens. 任何地方都没有错误,但是什么也没有发生。 If I log the socket variable I get this 如果我记录套接字变量,我得到这个
Is it possible to emit data from my back end Sails app to my front end Angular2 component? 是否可以将数据从后端Sails应用发送到前端Angular2组件?
您应该确保您加入了一个房间,广播方法仅广播到房间中所有连接的人
socket.join('roomName');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.