项目作者: alxshelepenok

项目描述 :
Some utilities for the development of socket.io application with InversifyJS.
高级语言: TypeScript
项目地址: git://github.com/alxshelepenok/inversify-socket-utils.git
创建时间: 2018-07-23T20:48:50Z
项目社区:https://github.com/alxshelepenok/inversify-socket-utils

开源协议:MIT License

下载


inversify-socket-utils

Some utilities for the development of socket.io applications with Inversify.

Installation

You can install inversify-socket-utils using npm:

  1. npm install inversify inversify-socket-utils reflect-metadata --save

Please refer to the InversifyJS documentation to learn more about the installation process.

How to use

Step 1: Create a controller.

  1. import "reflect-metadata";
  2. import { injectable } from "inversify";
  3. import { Socket } from "socket.io";
  4. import {
  5. connectedSocket,
  6. controller,
  7. onConnect,
  8. onDisconnect,
  9. onMessage,
  10. } from "inversify-socket-utils";
  11. @injectable()
  12. @controller("/namespace")
  13. export class MessageController {
  14. @onConnect("connection")
  15. connection() {
  16. console.log("client connected");
  17. }
  18. @onDisconnect("disconnect")
  19. disconnect() {
  20. console.log("client disconnected");
  21. }
  22. @onMessage("message")
  23. message(@connectedSocket() socket: Socket) {
  24. console.log("message received");
  25. socket.emit("message", "hello!");
  26. }
  27. }

Step 2: Configure container and server.

Configure the inversify container in your composition root as usual.

Then, pass the container to the InversifySocketServer constructor. This will allow it to register all controllers and their dependencies from your container and attach them to the socket.io app.
Then just call server.build() to prepare your app.

In order for the InversifySocketServer to find your controllers, you must bind them to the TYPE.Controller service identifier and tag the binding with the controller’s name.

  1. import { createServer } from "http";
  2. import { Container } from "inversify";
  3. import { Server } from "socket.io";
  4. import { MessageController } from "./controllers/message";
  5. import {
  6. interfaces,
  7. InversifySocketServer,
  8. TYPE,
  9. } from "inversify-socket-utils";
  10. const container = new Container();
  11. container.bind<interfaces.Controller>(TYPE.Controller).to(MessageController);
  12. const app = createServer();
  13. const io = new Server(app, {
  14. cors: {
  15. origin: "*",
  16. },
  17. });
  18. const server = new InversifySocketServer(container, io);
  19. server.build();
  20. app.listen(3000);
  21. console.log("Server is listening on port 3000");

License

The MIT License (MIT)

Copyright (c) 2025 Alexander Shelepenok

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.