UNPKG

react-cosmos

Version:

Sandbox for developing and testing UI components in isolation

30 lines (29 loc) 1.09 kB
import { serverSocketMessage } from 'react-cosmos-core'; import WebSocket, { WebSocketServer } from 'ws'; export function createMessageHandler(httpServer) { const wss = new WebSocketServer({ server: httpServer }); wss.on('connection', ws => { // Forward commands between connected clients. Parties involved can be the // - The Cosmos UI, which acts as a remote control // - The web iframe or the React Native component renderers ws.on('message', msg => { wss.clients.forEach(client => { if (client !== ws && client.readyState === WebSocket.OPEN) { client.send(msg.toString()); } }); }); }); function sendMessage(msg) { wss.clients.forEach(client => { if (client.readyState === WebSocket.OPEN) { client.send(JSON.stringify(serverSocketMessage(msg))); } }); } function close() { wss.clients.forEach(client => client.close()); wss.close(); } return { sendMessage, close }; }