Skip to content

Commit a3bdd41

Browse files
committed
option to autodisconnect
1 parent 2f63982 commit a3bdd41

File tree

6 files changed

+147
-107
lines changed

6 files changed

+147
-107
lines changed

examples/socket-chat/public/app.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
1-
(function () {
1+
import * as chain from '/chain.js'
22

3-
const socket = chain.Socket('/socket')
4-
socket.connect()
3+
const socket = new chain.Socket('/socket')
4+
socket.connect()
55

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 "))
1111

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')
1216

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()
1719

18-
$form.addEventListener('submit', function (e) {
19-
e.preventDefault()
20+
if ($inputName.value === '') {
21+
return
22+
}
2023

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
3127
})
3228

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+
})

examples/socket-chat/public/index.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ <h2 class="c1">&lt;<span class="c2">!{S}</span>yntax&gt; <em>Chain Chat</em>!</h
2222
</main>
2323
</div>
2424

25-
<script src="/chain.js"></script>
26-
<script src="/app.js"></script>
25+
<script src="/app.js" type="module"></script>
2726
</body>
2827
</html>

socket/client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
```
66
npm install -g typescript
7-
tsc chain.ts
7+
npx tsc
88
```

socket/client/chain.js

Lines changed: 53 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

socket/client/chain.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)