開発用に動作をモックするフェイクサーバを作る。まずは先行事例を見つつ、Websocketでメッセージを返すところから。
'use strict'; // ref: https://shizenkarasuzon.hatenablog.com/entry/2021/04/21/004132 const fs = require('fs'); const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8000 }); wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { console.log('received: %s', message); ws.send('something'); }); ws.send('something'); });