|
1 | | -(function () { |
| 1 | +import * as chain from '/chain.js' |
2 | 2 |
|
3 | | - const socket = chain.Socket('/socket') |
4 | | - socket.connect() |
| 3 | +const socket = new chain.Socket('/socket') |
| 4 | +socket.connect() |
5 | 5 |
|
6 | | - const channel = socket.channel("chat:lobby", {param1: 'foo'}) |
7 | | - channel.join() |
8 | | - .on('ok', () => chain.log('Join', "success")) |
9 | | - .on('error', err => chain.log('Join', "errored", err)) |
10 | | - .on('timeout', () => chain.log('Join', "timed out ")) |
| 6 | +const channel = socket.channel("chat:lobby", { param1: 'foo' }) |
| 7 | +channel.join() |
| 8 | + .on('ok', () => chain.log('Join', "success")) |
| 9 | + .on('error', err => chain.log('Join', "errored", err)) |
| 10 | + .on('timeout', () => chain.log('Join', "timed out ")) |
11 | 11 |
|
| 12 | +const $form = document.getElementById('chat-form') |
| 13 | +const $chatBox = document.getElementById('chat-box') |
| 14 | +const $inputName = document.getElementById('user-name') |
| 15 | +const $inputMessage = document.getElementById('user-msg') |
12 | 16 |
|
13 | | - const $form = document.getElementById('chat-form') |
14 | | - const $chatBox = document.getElementById('chat-box') |
15 | | - const $inputName = document.getElementById('user-name') |
16 | | - const $inputMessage = document.getElementById('user-msg') |
| 17 | +$form.addEventListener('submit', function (e) { |
| 18 | + e.preventDefault() |
17 | 19 |
|
18 | | - $form.addEventListener('submit', function (e) { |
19 | | - e.preventDefault() |
| 20 | + if ($inputName.value === '') { |
| 21 | + return |
| 22 | + } |
20 | 23 |
|
21 | | - if ($inputName.value === '') { |
22 | | - return |
23 | | - } |
24 | | - |
25 | | - channel.push('shout', { |
26 | | - name: $inputName.value, |
27 | | - body: $inputMessage.value |
28 | | - }) |
29 | | - |
30 | | - $inputMessage.value = '' |
| 24 | + channel.push('shout', { |
| 25 | + name: $inputName.value, |
| 26 | + body: $inputMessage.value |
31 | 27 | }) |
32 | 28 |
|
33 | | - channel.on('shout', (message, ref, joinRef) => { |
34 | | - let $p = document.createElement('p') |
35 | | - $p.classList.add('c1') |
36 | | - $p.insertAdjacentHTML('beforeend', `<b class="c2">${message.name}:</b> ${message.body}`) |
37 | | - $chatBox.appendChild($p) |
38 | | - }) |
39 | | -})() |
| 29 | + $inputMessage.value = '' |
| 30 | +}) |
| 31 | + |
| 32 | +channel.on('shout', (message, ref, joinRef) => { |
| 33 | + let $p = document.createElement('p') |
| 34 | + $p.classList.add('c1') |
| 35 | + $p.insertAdjacentHTML('beforeend', `<b class="c2">${message.name}:</b> ${message.body}`) |
| 36 | + $chatBox.appendChild($p) |
| 37 | +}) |
0 commit comments