0.1.1 • Published 6 years ago

webhost-websocket v0.1.1

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

webhost-websocket

A WebSocket implemantation for WebHost.

Write using TypeScript with Visual Studio Code.

import { HttpApplication, IHttpApplication, IConfigure, IConfigureServices,
    NotFound, DefaultFiles, StaticFiles, IContext } from 'webhost';
import { WebSocketModule, WebSocketService } from 'webhost-websocket';

import { ChatHub } from './chathub';

@HttpApplication({
    imports: [WebSocketModule],
    providers: [ChatHub],
    port: 1800,
    wwwroot: __dirname + '/wwwroot'
})
export class HttpTestApplication implements IHttpApplication {

    public constructor(
        private webSocketService: WebSocketService
    ) {
    }

    public configureServices(services: IConfigureServices): void {

        this.webSocketService.configureWebSocket(services);
    }

    public configure(app: IConfigure): void {
     
        app.use((ctx: IContext, next: () => void) => {
            if (ctx.request.url && ctx.request.url.startsWith('/node_modules/')) {
                ctx.request.url = '../../..' + ctx.request.url;
            }
            next();
        });

        app.use(DefaultFiles);

        app.use(StaticFiles);

        app.use(NotFound);
    }
}
import { HostService, Path } from 'webhost-websocket';

@Path({
    name: 'chat'
})
export class ChatHub {

    public constructor(
        private host: HostService
    ) {
        setTimeout(() => {
            this.host.callr<string>('getclient')
                .then(d => console.log('client: ' + d));
        }, 3000);
    }

    public send(user: string, msg: string): void {
        this.receive(user, msg);
    }

    public receive(user: string, msg: string): void {
        this.host.callAll('receive', user, msg);
    }
}

Installation

$ npm install webhost-websocket

Features

  • Lightweight

Docs

Quick Start

Create a empty NPM package:

$ npm init -y

Install WebHost:

$ npm install webhost-websocket --save 

Work with code:

$ code .

Example

https://github.com/Cliveburr/WebHost/tree/master/test/websocket
0.1.1

6 years ago

0.0.4

6 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago