~ev/tildeprotocol

9aac1d666f64acbcf2cc23ecc4bf1c835c6c6d83 — Ev Bogue 4 years ago febc5d6
connect, disconnect messages
1 files changed, 15 insertions(+), 5 deletions(-)

M chat.js
M chat.js => chat.js +15 -5
@@ 17,6 17,7 @@ var PORT = process.argv[2] || 3333
var wss = new WS.Server({ port: PORT})

wss.on('connection', function (ws) {
  console.log(ws)
  ws.on('message', function (data) {
    wss.clients.forEach(function (client) {
      if (client.readyState === WS.OPEN) {


@@ 43,6 44,7 @@ tilde.keys().then(keys => {
    var msg = fs.readFileSync(__dirname + '/tildes/' + db[i], 'UTF-8')
    tilde.open(msg, keys).then(opened => {
      var tilde = opened.split('~')
      console.log(tilde)
      var msg = ''
      for (i = 0; i < tilde.length - 3; i++) {
        msg = msg + tilde[i] + ' - '


@@ 53,6 55,7 @@ tilde.keys().then(keys => {
  }

  ws.on('open', function () {

    ws.on('message', function (message) {
      tilde.hash(message).then(hash => {
        db.push(hash)


@@ 60,7 63,7 @@ tilde.keys().then(keys => {
      })
      tilde.open(message, keys).then(opened => {
        var tilde = opened.split('~')
        var msg = '' 
        var msg = ''
        for (i = 0; i < tilde.length - 3; i++) {
          msg = msg + tilde[i] + ' - '
        }


@@ 72,7 75,6 @@ tilde.keys().then(keys => {
    })

    console.log(keys)
    
    var home

    rl.question('Choose a nickname > ', function (nick) {


@@ 80,6 82,9 @@ tilde.keys().then(keys => {
      //rl.setPrompt(nick + ' > ')
      rl.question('Set a home server > ', function (server) {
        home = nick + '@' + server
        tilde.publish(home + ' connected', null, home, keys).then(msg => {
          ws.send(msg)
        })
        rl.prompt()
      })
    })


@@ 90,9 95,14 @@ tilde.keys().then(keys => {
      })
    }).on('close', () => {
      fs.writeFileSync(__dirname + '/db', JSON.stringify(db), 'UTF-8')
      console.log('\nWriting db to disk')
      console.log('\nExiting Tilde')
      process.exit(0)
      tilde.publish(home + ' disconnected', null, home, keys).then(msg => {
        ws.send(msg)
        console.log('\nWriting db to disk')
        console.log('\nExiting Tilde')
        setTimeout(function () {
          process.exit(0)
        }, 1000)
      })
    }) 
  }) 
})