File tree 5 files changed +53
-29
lines changed
5 files changed +53
-29
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ of a simple chat webapp using nhooyr.io/websocket.
5
5
6
6
``` bash
7
7
$ cd example
8
- $ go run .
8
+ $ go run . localhost:0
9
9
listening on http://127.0.0.1:51055
10
10
```
11
11
Original file line number Diff line number Diff line change 1
1
body {
2
2
width : 100vw ;
3
- height : 100vh ;
4
3
min-width : 320px ;
5
4
}
6
5
7
6
# root {
8
- padding : 20px ;
9
- max-width : 500 px ;
7
+ padding : 40 px 20px ;
8
+ max-width : 480 px ;
10
9
margin : auto;
11
- max- height: 100vh ;
10
+ height : 100vh ;
12
11
13
12
display : flex;
14
13
flex-direction : column;
@@ -20,42 +19,57 @@ body {
20
19
margin : 20px 0 0 0 ;
21
20
}
22
21
22
+ /* 100vh on safari does not include the bottom bar. */
23
+ @supports (-webkit-overflow-scrolling : touch) {
24
+ # root {
25
+ height : 85vh ;
26
+ }
27
+ }
28
+
23
29
# message-log {
24
30
width : 100% ;
25
- height : 100vh ;
26
31
flex-grow : 1 ;
27
32
overflow : auto;
28
33
}
29
34
30
35
# message-log p : first-child {
31
- margin-top : 0 ;
32
- margin-bottom : 0 ;
36
+ margin : 0 ;
33
37
}
34
38
35
39
# message-log > * + * {
36
40
margin : 10px 0 0 0 ;
37
41
}
38
42
39
- # publish-form {
40
- appearance : none;
43
+ # publish-form-container {
44
+ width : 100% ;
45
+ }
41
46
42
- display : flex;
43
- align-items : center;
44
- justify-content : center;
47
+ # publish-form {
45
48
width : 100% ;
49
+ display : flex;
50
+ height : 40px ;
51
+ }
52
+
53
+ # publish-form > * + * {
54
+ margin : 0 0 0 10px ;
46
55
}
47
56
48
57
# publish-form input [type = "text" ] {
49
58
flex-grow : 1 ;
59
+
60
+ -moz-appearance : none;
61
+ -webkit-appearance : none;
50
62
word-break : normal;
51
63
border-radius : 5px ;
64
+ border : 1px solid # ccc ;
52
65
}
53
66
54
67
# publish-form input [type = "submit" ] {
55
68
color : white;
56
69
background-color : black;
57
70
border-radius : 5px ;
58
- margin-left : 10px ;
71
+ padding : 5px 10px ;
72
+ border : none;
59
73
}
60
74
61
75
# publish-form input [type = "submit" ]: hover {
Original file line number Diff line number Diff line change 5
5
< title > nhooyr.io/websocket - Chat Example</ title >
6
6
< meta name ="viewport " content ="width=device-width " />
7
7
8
- < link href ="/index.css " rel ="stylesheet " />
9
8
< link href ="https://unpkg.com/sanitize.css " rel ="stylesheet " />
10
9
< link href ="https://unpkg.com/sanitize.css/typography.css " rel ="stylesheet " />
11
10
< link href ="https://unpkg.com/sanitize.css/forms.css " rel ="stylesheet " />
11
+ < link href ="/index.css " rel ="stylesheet " />
12
12
</ head >
13
13
< body >
14
14
< div id ="root ">
15
15
< div id ="message-log "> </ div >
16
- < form id ="publish-form ">
17
- < input name ="message " id ="message-input " type ="text " />
18
- < input type ="submit " />
19
- </ form >
16
+ < div id ="publish-form-container ">
17
+ < form id ="publish-form ">
18
+ < input name ="message " id ="message-input " type ="text " />
19
+ < input type ="submit " />
20
+ </ form >
21
+ </ div >
20
22
</ div >
21
23
< script type ="text/javascript " src ="/index.js "> </ script >
22
24
</ body >
Original file line number Diff line number Diff line change 4
4
function dial ( ) {
5
5
conn = new WebSocket ( `ws://${ location . host } /subscribe` )
6
6
7
- conn . addEventListener ( "close" , ( ev ) => {
8
- console . error ( "subscribe WebSocket closed" , ev )
9
- console . info ( "reconnecting in 1000ms" , ev )
7
+ conn . addEventListener ( "close" , ev => {
8
+ console . info ( "websocket disconnected, reconnecting in 1000ms" , ev )
10
9
setTimeout ( dial , 1000 )
11
10
} )
11
+ conn . addEventListener ( "open" , ev => {
12
+ console . info ( "websocket connected" )
13
+ } )
12
14
conn . addEventListener ( "message" , ev => {
13
15
if ( typeof ev . data !== "string" ) {
14
16
console . error ( "unexpected message type" , typeof ev . data )
15
17
return
16
18
}
17
- appendLog ( ev . data )
19
+ const p = appendLog ( ev . data )
18
20
if ( expectingMessage ) {
19
- messageLog . scrollTo ( 0 , messageLog . scrollHeight )
21
+ p . scrollIntoView ( )
20
22
expectingMessage = false
21
23
}
22
24
} )
31
33
const p = document . createElement ( "p" )
32
34
p . innerText = `${ new Date ( ) . toLocaleTimeString ( ) } : ${ text } `
33
35
messageLog . append ( p )
36
+ return p
34
37
}
35
38
appendLog ( "Submit a message to get started!" )
36
39
47
50
fetch ( "/publish" , {
48
51
method : "POST" ,
49
52
body : msg ,
50
- } ) . catch ( err => {
51
- console . error ( "failed to publish" , err )
52
53
} )
53
54
}
54
55
} ) ( )
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
- "fmt "
4
+ "errors "
5
5
"log"
6
6
"net"
7
7
"net/http"
8
+ "os"
8
9
"time"
9
10
)
10
11
11
12
func main () {
13
+ log .SetFlags (0 )
14
+
12
15
err := run ()
13
16
if err != nil {
14
17
log .Fatal (err )
15
18
}
16
19
}
17
20
18
21
func run () error {
19
- l , err := net .Listen ("tcp" , "localhost:0" )
22
+ if len (os .Args ) < 2 {
23
+ return errors .New ("please provide an address to listen on as the first argument" )
24
+ }
25
+
26
+ l , err := net .Listen ("tcp" , os .Args [1 ])
20
27
if err != nil {
21
28
return err
22
29
}
23
- fmt .Printf ("listening on http://%v\n " , l .Addr ())
30
+ log .Printf ("listening on http://%v" , l .Addr ())
24
31
25
32
var ws chatServer
26
33
You can’t perform that action at this time.
0 commit comments