webhost-websocket
v0.1.1
Published
WebSocket implemantation for WebHost.
Downloads
1
Readme
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
- Github for Official Code
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