WebSocketHandlerTest.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const { Server, WebSocketHandler } = require('../dist');
  2. class WSHandler0 extends WebSocketHandler {
  3. constructor() {
  4. super(...arguments);
  5. this.onConnect = (ws) => {
  6. console.log('ws0 connection:', ws);
  7. }
  8. this.onMessage = (message) => {
  9. console.log('ws0 message:', message.toString());
  10. }
  11. this.onError = () => {
  12. console.log('ws0 error');
  13. }
  14. this.onClose = (code, reason) => {
  15. console.log('ws0 closed', code, reason.toString());
  16. }
  17. }
  18. }
  19. class WSHandler1 extends WebSocketHandler {
  20. constructor() {
  21. super(...arguments);
  22. this.onConnect = (ws) => {
  23. console.log('ws1 connection:', ws);
  24. }
  25. this.onMessage = (message) => {
  26. console.log('ws1 message:', message.toString());
  27. }
  28. this.onError = () => {
  29. console.log('ws1 error');
  30. }
  31. this.onClose = (code, reason) => {
  32. console.log('ws1 closed', code, reason.toString());
  33. }
  34. }
  35. }
  36. new Server({ port: 3001, wsHandlers: { '/ws0': new WSHandler0(), '/ws1': new WSHandler1() } }).init().then((server) => {
  37. server.start();
  38. });