1.0.13 • Published 3 years ago
@happymonkey/websocket-client v1.0.13
websocket-client
Install
npm i @happymonkey/websocket-client
Include in your app.module.ts
:
...
import {WebSocketClientModule} from 'web-socket-client';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
...
WebSocketClientModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Example
...
import {WebSocketClientService} from 'web-socket-client';
...
constructor( public ws: WebSocketClientService ) {
// Subscribe to events
this.ws.onOpen.subscribe(() => {
console.log( 'Server is ready');
});
this.ws.onError.subscribe((error) => {
console.error(error);
});
this.ws.onClose.subscribe(() => {
console.log( 'Connection closed' );
});
this.ws.onMessage.subscribe(( message: WebSocketMessage ) => {
console.log( message.action, message.data );
});
this.ws.on('action_name').subscribe(( data: any ) => {
console.log( 'Action received from server with data :', data );
});
this.ws.onTextMessage.subscribe(( payload: string ) => {
console.log( 'Text message received :', payload );
});
// Connect to server
this.ws.open('ws://localhost:8282');
// Send action
this.ws.send('action', {foo: 'bar'});
// Send plain text message
this.ws.sendText('plain text message');
// Close connection
this.ws.close();
// Check if client is connected
if ( this.ws.opened ) {
console.log( 'WebSocket is ready' );
}
// Properties
const url = this.ws.url;
const readyState = this.ws.readyState;
}
1.0.13
3 years ago
1.0.12
3 years ago
1.0.11
3 years ago
1.0.10
3 years ago
1.0.9
4 years ago
1.0.8
4 years ago
1.0.7
4 years ago
1.0.6
4 years ago
1.0.5
4 years ago
1.0.4
4 years ago
1.0.3
4 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago
0.0.5
4 years ago
0.0.3
4 years ago
0.0.4
4 years ago
0.0.2
4 years ago
0.0.1
4 years ago