UNPKG

contact

Version:

private, one-to-one or many-to-many command line chat

50 lines (42 loc) 1.47 kB
#!/usr/bin/env node import Client from 'contact' import { streamReadText } from 'stream-read-all' import CliView from '../lib/cli-view.js' // import Transmission from 'contact-model/transmission' const url = process.argv[2] || 'http://127.0.0.1:9000' const userid = process.argv[3] || 'user1' const roomid = process.argv[4] || 'room1' const auto = process.argv[5] === 'auto' const view = new CliView() const client = new Client() let connection // let crsId try { connection = await client.connect(url) client.addEventListener('close', async e => { const cause = e.detail await view.displayMessage(`Connection closed, cause: ${cause}`, { skipPrompt: true }) view.close() // console.log('Connection to relay closed, shutting down.') }) client.addEventListener('msg', async e => { const t = e.detail const msg = await streamReadText(t.readable) await view.displayMessage(`${t.headers.from}: ${msg}`, { skipPrompt: auto }) }) client.addEventListener('notification', async e => { await view.displayNotification(e.detail.subType, e.detail) }) // await connection.joinRoom(userid, roomid) setInterval(async () => { await connection.sendTextMsg(userid, roomid, `Auto: ${(Math.random() * 100).toFixed(0)}`) }, 1000) } catch (err) { await connection?.close() await client.close() if (err?.cause?.code === 'ABORT_ERR') { console.error('Connection attempt timed out') } else { console.error(err) } }