Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,857 changes: 0 additions & 5,857 deletions package-lock.json

This file was deleted.

42 changes: 20 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,29 @@
"directory": "/"
},
"dependencies": {
"@loopback/boot": "^2.4.0",
"@loopback/core": "^2.9.2",
"@loopback/repository": "^2.10.0",
"@loopback/rest": "^5.2.1",
"@loopback/rest-explorer": "^2.2.7",
"@loopback/service-proxy": "^2.3.5",
"@types/socket.io": "^2.1.10",
"@types/socket.io-client": "^1.4.33",
"loopback-connector-rest": "^3.7.0",
"@loopback/boot": "^5.0.0",
"@loopback/core": "^4.0.0",
"@loopback/http-server": "^4.0.0",
"@loopback/repository": "^5.0.0",
"@loopback/rest": "^12.0.0",
"@loopback/rest-explorer": "^5.0.0",
"@loopback/service-proxy": "^5.0.0",
"loopback-connector-rest": "^4.0.1",
"morgan": "^1.10.0",
"socket.io": "^2.3.0",
"tslib": "^2.0.0"
"socket.io": "^4.5.1",
"tslib": "^2.4.0"
},
"devDependencies": {
"@loopback/build": "^6.1.1",
"@loopback/eslint-config": "^8.0.4",
"@loopback/http-caching-proxy": "^2.1.10",
"@loopback/testlab": "^3.2.1",
"@types/lodash": "^4.14.157",
"@types/morgan": "^1.9.1",
"@types/node": "^10.17.27",
"eslint": "^7.5.0",
"lodash": "^4.17.19",
"socket.io-client": "^2.3.0",
"typescript": "~3.9.7"
"@loopback/build": "^9.0.0",
"@loopback/eslint-config": "^13.0.0",
"@loopback/http-caching-proxy": "^4.0.0",
"@loopback/testlab": "^5.0.0",
"@types/lodash": "^4.14.182",
"@types/morgan": "^1.9.3",
"@types/node": "^17.0.35",
"eslint": "^8.16.0",
"lodash": "^4.17.21",
"typescript": "~4.6.4"
},
"keywords": [
"loopback",
Expand Down
2 changes: 1 addition & 1 deletion public/chats.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<button>Send</button>
</form>
<button id="emitRoomEvent">Emit room event</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.1/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/chat.controller.ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export class ChatControllerWs {
* @param socket
*/
@ws.connect()
connect(socket: Socket) {
async connect(socket: Socket) {
console.log('Client connected: %s', this.socket.id);
socket.join('room 1');
await socket.join('room 1');
// Room notification of request /todos/room/example/emit (TodoController)
socket.join('some room');
await socket.join('some room');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (require.main === module) {
const config = {
rest: {
port,
host: process.env.HOST ?? 'localhost',
host: '0.0.0.0',
openApiSpec: {
// useful when used with OpenAPI-to-GraphQL to locate your application
setServersFromRequest: true,
Expand Down
11 changes: 5 additions & 6 deletions src/websockets/websocket-controller-factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BindingScope, Constructor, Context, invokeMethod, MetadataInspector, } from '@loopback/context';
import { Constructor, Context, invokeMethod, MetadataInspector, } from '@loopback/context';
import { Socket } from 'socket.io';

/* eslint-disable @typescript-eslint/no-misused-promises */
Expand All @@ -12,8 +12,7 @@ export class WebSocketControllerFactory {
this.ctx
.bind('ws.controller')
.toClass(this.controllerClass)
.tag('websocket')
.inScope(BindingScope.CONTEXT);
.tag('websocket');
}

async create(socket: Socket) {
Expand All @@ -30,7 +29,7 @@ export class WebSocketControllerFactory {
MetadataInspector.getAllMethodMetadata(
'websocket:connect',
this.controllerClass.prototype,
) || {};
) ?? {};
for (const m in connectMethods) {
await invokeMethod(this.controller, m, this.ctx, [socket]);
}
Expand All @@ -43,13 +42,13 @@ export class WebSocketControllerFactory {
MetadataInspector.getAllMethodMetadata<(string | RegExp)[]>(
'websocket:subscribe',
this.controllerClass.prototype,
) || {};
) ?? {};
for (const m in subscribeMethods) {
for (const t of subscribeMethods[m]) {
const regexps: RegExp[] = [];
if (typeof t === 'string') {
socket.on(t, async (...args: unknown[]) => {
let done: Function = (result: any) => null;
let done: Function = () => null;
if (typeof args[args.length - 1] === 'function') {
done = args.pop() as Function;
}
Expand Down
10 changes: 4 additions & 6 deletions src/websockets/websocket.application.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ApplicationConfig } from '@loopback/core';
import { HttpServer } from '@loopback/http-server';
import { RestApplication } from '@loopback/rest';
import { WebSocketServer } from "./websocket.server";
import { Constructor } from "@loopback/context";
Expand All @@ -8,22 +7,21 @@ import { Namespace } from "socket.io";
export { ApplicationConfig };

export class WebsocketApplication extends RestApplication {
readonly httpServer: HttpServer;
readonly wsServer: WebSocketServer;

constructor(options: ApplicationConfig = {}) {
super(options);
this.httpServer = new HttpServer(this.requestHandler, options.websocket);
this.wsServer = new WebSocketServer(this, this.httpServer);
this.wsServer = new WebSocketServer(this, options?.socketio);
}

public websocketRoute(controllerClass: Constructor<any>, namespace?: string | RegExp): Namespace {
public websocketRoute(controllerClass: Constructor<{ [method: string]: Function }>, namespace?: string | RegExp): Namespace {
return this.wsServer.route(controllerClass, namespace) as Namespace;
}

public async start(): Promise<void> {
await this.wsServer.start();
await super.start();
const httpServer = this.restServer.httpServer?.server;
await this.wsServer.start(httpServer!);
}

public async stop(): Promise<void> {
Expand Down
27 changes: 11 additions & 16 deletions src/websockets/websocket.server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Constructor, Context } from '@loopback/context';
import { HttpServer } from '@loopback/http-server';
import { Server, ServerOptions, Socket } from 'socket.io';
import { WebSocketControllerFactory } from './websocket-controller-factory';
import { getWebSocketMetadata, WebSocketMetadata } from "./decorators/websocket.decorator";
import SocketIOServer = require("socket.io");
import http from 'http';
import https from 'https';

const debug = require('debug')('loopback:websocket');

Expand All @@ -21,11 +21,10 @@ export class WebSocketServer extends Context {

constructor(
public ctx: Context,
public readonly httpServer: HttpServer,
private options: ServerOptions = {},
private options?: ServerOptions,
) {
super(ctx);
this.io = SocketIOServer(options);
this.io = new Server(options);
ctx.bind('ws.server').to(this.io);
}

Expand All @@ -42,15 +41,15 @@ export class WebSocketServer extends Context {
* @param ControllerClass
* @param meta
*/
route(ControllerClass: Constructor<any>, meta?: WebSocketMetadata | string | RegExp) {
route(controllerClass: Constructor<{ [method: string]: Function }>, meta?: WebSocketMetadata | string | RegExp) {
if(meta instanceof RegExp || typeof meta === 'string'){
meta = { namespace: meta } as WebSocketMetadata;
}
if (meta == null) {
meta = getWebSocketMetadata(ControllerClass) as WebSocketMetadata;
meta = getWebSocketMetadata(controllerClass) as WebSocketMetadata;
}
const nsp = (meta && meta.namespace) ? this.io.of(meta.namespace) : this.io;
if (meta && meta.name) {
const nsp = meta?.namespace ? this.io.of(meta.namespace) : this.io;
if (meta?.name) {
this.ctx.bind(`ws.namespace.${meta.name}`).to(nsp);
}

Expand All @@ -67,7 +66,7 @@ export class WebSocketServer extends Context {
// Bind websocket
reqCtx.bind('ws.socket').to(socket);
// Instantiate the controller instance
await new WebSocketControllerFactory(reqCtx, ControllerClass).create(
await new WebSocketControllerFactory(reqCtx, controllerClass).create(
socket,
);
});
Expand All @@ -77,11 +76,8 @@ export class WebSocketServer extends Context {
/**
* Start the websocket server
*/
async start() {
await this.httpServer.start();
// FIXME: Access HttpServer.server
const server = (this.httpServer as any).server;
this.io.attach(server, this.options);
async start(httpServer: http.Server | https.Server) {
this.io.attach(httpServer, this.options);
}

/**
Expand All @@ -94,6 +90,5 @@ export class WebSocketServer extends Context {
});
});
await close;
await this.httpServer.stop();
}
}
Loading