ws-wrapper-sofair

ws-wrapper

Usage no npm install needed!

<script type="module">
  import wsWrapperSofair from 'https://cdn.skypack.dev/ws-wrapper-sofair';
</script>

README

USAGE

Server:

import * as http from 'http'; import { default as WebSocket } from './components/websocketWrapper'; import { default as registerListeners } from './listeners'; const server = http.createServer(); const webSocketServer = new WebSocket({ isServer: true, config: { server } }); webSocketServer.onConnect((client) => { client.emitter.on('ping', () => { webSocketServer.emit('pong', {}, client); }); }); server.listen(8080);

Client:

let counter = 0;

import { default as WebSocket } from 'ws-wrapper-sofair'; const webSocket = new WebSocket({ url: 'ws://localhost:8080' });

webSocket.onConnect(() => { console.log(new Date()); console.log('connected'); webSocket.emit('ping', { timestamp: Date.now() }); });

webSocket.onDisconnect(() => { console.log('disconnected'); console.log(new Date()); console.log(counter); });

webSocket.on('pong', (data: any) => { counter += 1; webSocket.emit('ping', { timestamp: Date.now() }); });