简体   繁体   English

使用inversify-express-utils,如何集成WebSocket?

[英]Using inversify-express-utils, how can i integrate websockets?

We try to connect to extend our typescript application based on inversify-express-utils with websockets , but no luck so far: 我们尝试通过websockets连接基于inversify-express-utils扩展我们的打字稿应用程序,但是到目前为止还没有运气:

import 'reflect-metadata';
import {interfaces, InversifyExpressServer, TYPE} from 'inversify-express-utils';
import { Kernel } from 'inversify';
import {UserController} from './controller/UserController';
import TYPES from './constant/types';
import * as socketIo from 'socket.io';

// set up kernel
let kernel = new Kernel();

// create server
let server = new InversifyExpressServer(kernel);
kernel.bind<interfaces.Controller>(TYPE.Controller).to(UserController).whenTargetNamed(TAGS.UserController);
server
  .build()
  .listen(3000, 'localhost', () => console.log('listening on http://localhost:3000'));

// try to setup a socket io based websocket
let io = socketIo(server);
io.on('connect', (socket) => console.log('"connect" executed'));
io.on('connection', (socket) => console.log('"connection" executed'));

Server starts up as expected with ts-node server.ts . 服务器将通过ts-node server.ts But I can't connect to this web socket. 但是我无法连接到此网络套接字。 Tried it with several tools, like for example Dark WebSocket Terminal chrome plugin and executing \\connect localhost:3000 . 使用多种工具进行了尝试,例如Dark WebSocket Terminal chrome插件和执行\\connect localhost:3000 Any idea? 任何想法?

I've got some similar issue some time ago. 我前段时间也遇到过类似的问题。 Here is what was worked for me: 这是为我工作的东西:

let server: interfaces.InversifyExpressServer = new InversifyExpressServer(kernel);

server.setConfig((app) => {
    ... 
    app.use(helmet());
    ...
});

let app = server.build();

let instance: any = app.listen(config.url.port);
console.log(`Server started on port ${config.url.port} :)`);


let socketIO = SocketIO.listen(instance);
socketIO.on('connection', (socket: SocketIO.Server) => {
    socket.emit('news', { hello: 'world' });
    socket.on('my other event', function (data: any) {
        console.log(data);
    });
});

exports = module.exports = app;

As you can see, you have to create a running instance of your express server before you can attach Socket.IO on to it. 如您所见,您必须先创建一个正在运行的Express服务器实例,然后才能将Socket.IO附加到该实例上。 Your server variable is not enough. 您的server变量还不够。 I hope this will work for you. 希望这对您有用。

Best regards 最好的祝福
.... Simon ....西蒙

A working example can be found on https://github.com/dkellenb/inversify-express-utils-websockets The issue was: 可以在https://github.com/dkellenb/inversify-express-utils-websockets上找到一个工作示例。问题是:

  • Server wrapper needs to be created around the app 需要围绕应用创建服务器包装
  • Listen should be started on the on the express server 侦听应在快递服务器上开始
  • Test connection string should look like this: 测试连接字符串应如下所示:
    ws://localhost:3000/socket.io/?EIO=3&transport=websocket

Code snippet: 程式码片段:

// server.build() actually returns an Express.Application instance
let app = server.build();

// socket.io expects an http.Server versus a InversifyExpressServer
// see http://socket.io/docs/#using-with-express-3/4
let httpServer = require('http').createServer(app);
httpServer.listen(3000);

// create socket.io
let io = socketIo(httpServer);
import * as express from 'express';
import * as expressWs from 'express-ws';
import { InversifyExpressServer } from 'inversify-express-utils';
import { container } from "./utils/InversifyConfig";

let inversifyExpressServer = new InversifyExpressServer(container);
  inversifyExpressServer.setConfig((app: expressWs.Application) => {
     expressWs(app);
     // Your other config
  });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 inversify-express-utils 控制器全局捕获错误 - Unable to catch errors globally from inversify-express-utils controllers 关于用于 inversify-express-utils 的良好 Node Express 多部分文件上传中间件的建议 controller - Advice on good Node Express Multipart File Upload middleware for inversify-express-utils controller 如何修复错误[ERR_ASSERTION]:构造函数已经存在! (“inversify-restify-utils”:“^ 3.4.0”)和(“inversify-restify-utils”:“^ 3.4.0”) - How to fixed the error [ERR_ASSERTION]: Constructor already exists! (“inversify-restify-utils”: “^3.4.0”) and ( “inversify-restify-utils”: “^3.4.0”) 如何将更快的语言与Express.js后端集成在一起? - How can I integrate a faster language with an Express.js backend? 反转快递订单路线 - Inversify Express order routes 如何使用Node在同一端口上使用带有Express的Websocket来使用HTTP? - How do I get HTTPs w/ Express working with websockets on the same port using node? 如何使用connect-sqlite3与express和websocket共享会话? - How do I share sessions with express and websockets using connect-sqlite3? 使用 express 的 websockets 连接 - Connections using websockets using express 如何使用 node 和 express-ws 在 websockets 中进行身份验证 - How to authenticate in websockets, using node and express-ws 如何使用Node.js和Websockets创建Twitter流? - How can I create a Twitter stream using Node.js and Websockets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM