Meteor-angular2 with Socket.io



我想将socket.io包含在我的Meteor-Angular2应用中。但是我发现添加socket.io/socketjs的困难。

我正在尝试探索此https://github.com/akryum/meteor-socket-io但这不是Meteor-Angular2。

在服务器/main.ts

上尝试此简单代码

var io = require('socket.io')(server); io.on('connection', function(socket) { socket.emit('welcome', { message: 'Welcome!', id: socket.id }); });

然后在控制台上显示此错误

错误:找不到模块'./client' W20170307-11:49:09.344(7)?(stderr)需要 (packages 模块 - runtime.js:123:19)W20170307-11:49:09.344(7)? (stderr)在eteorinstall.node_modules.socket.io.lib.index.js (包模块:1131:14)W20170307-11:49:09.345(7)?(stderr at FileValuate(packages 模块 - runtime.js:197:9) W20170307-11:49:09.345(7)?(stderr)需要 (packages 模块 - runtime.js:120:16)

任何人都可以共享配置,设置或样板以加入Meteor-Angular2和socket.io?

要在客户端使用套接字,您必须先安装socket.io-client,然后需要在组件或服务中导入它以使用它。

使用npm install socket.io-client --save安装socket.io-client

import * as io from 'socket.io-client';

将其导入

与组件中的连接套接字一起使用以下代码:

let socket = io.connect('your socket url', {
  forceNew  : true,
  transports: ['websocket', 'polling', 'flashsocket']
});

最新更新