1- import { Socket , Presence } from " phoenix"
1+ import { Socket , Presence } from ' phoenix' ;
22
33export async function connectChat ( ) {
4- const viewercount = document . getElementById ( " viewercount" ) ;
5- const chatMessages = document . getElementById ( " chat-messages" ) ;
6- const chatInput = document . getElementById ( " chat-input" ) ;
7- const chatNickname = document . getElementById ( " chat-nickname" ) ;
8- const chatButton = document . getElementById ( " chat-button" ) ;
4+ const viewercount = document . getElementById ( ' viewercount' ) ;
5+ const chatMessages = document . getElementById ( ' chat-messages' ) ;
6+ const chatInput = document . getElementById ( ' chat-input' ) ;
7+ const chatNickname = document . getElementById ( ' chat-nickname' ) ;
8+ const chatButton = document . getElementById ( ' chat-button' ) ;
99
10- let socket = new Socket ( " /socket" , { params : { token : window . userToken } } ) ;
10+ let socket = new Socket ( ' /socket' , { params : { token : window . userToken } } ) ;
1111
1212 socket . connect ( ) ;
1313
14- const channel = socket . channel ( " stream:chat" ) ;
14+ const channel = socket . channel ( ' stream:chat' ) ;
1515 const presence = new Presence ( channel ) ;
1616
17- send = function ( ) {
18- body = chatInput . value . trim ( ) ;
19- if ( body != "" ) {
20- channel . push ( " chat_msg" , { body : body } ) ;
21- chatInput . value = "" ;
17+ const send = function ( ) {
18+ const body = chatInput . value . trim ( ) ;
19+ if ( body != '' ) {
20+ channel . push ( ' chat_msg' , { body : body } ) ;
21+ chatInput . value = '' ;
2222 }
23- }
23+ } ;
2424
2525 presence . onSync ( ( ) => {
2626 viewercount . innerText = presence . list ( ) . length ;
2727 } ) ;
2828
29- channel . join ( )
30- . receive ( "ok" , resp => { console . log ( "Joined chat channel successfully" , resp ) } )
31- . receive ( "error" , resp => { console . log ( "Unable to join chat channel" , resp ) } ) ;
29+ channel
30+ . join ( )
31+ . receive ( 'ok' , ( resp ) => {
32+ console . log ( 'Joined chat channel successfully' , resp ) ;
33+ } )
34+ . receive ( 'error' , ( resp ) => {
35+ console . log ( 'Unable to join chat channel' , resp ) ;
36+ } ) ;
3237
33- channel . on ( " join_chat_resp" , resp => {
38+ channel . on ( ' join_chat_resp' , ( resp ) => {
3439 if ( resp . result === 'success' ) {
35- chatButton . innerText = " Send" ;
40+ chatButton . innerText = ' Send' ;
3641 chatButton . onclick = send ;
3742 chatNickname . disabled = true ;
3843 chatInput . disabled = false ;
@@ -42,13 +47,13 @@ export async function connectChat() {
4247 ev . preventDefault ( ) ;
4348 send ( ) ;
4449 }
45- }
50+ } ;
4651 } else {
4752 chatNickname . classList . add ( 'invalid-input' ) ;
4853 }
4954 } ) ;
5055
51- channel . on ( " chat_msg" , msg => {
56+ channel . on ( ' chat_msg' , ( msg ) => {
5257 if ( msg . nickname == undefined || msg . body == undefined ) return ;
5358
5459 const chatMessage = document . createElement ( 'div' ) ;
@@ -71,15 +76,15 @@ export async function connectChat() {
7176
7277 // allow for 1 scroll history
7378 if ( chatMessages . scrollHeight > 2 * chatMessages . clientHeight ) {
74- chatMessages . removeChild ( chatMessages . children [ 0 ] ) ;
79+ chatMessages . removeChild ( chatMessages . children [ 0 ] ) ;
7580 }
76- } )
81+ } ) ;
7782
7883 chatButton . onclick = ( ) => {
79- channel . push ( " join_chat" , { nickname : chatNickname . value } ) ;
84+ channel . push ( ' join_chat' , { nickname : chatNickname . value } ) ;
8085 } ;
8186
8287 chatNickname . onclick = ( ) => {
83- chatNickname . classList . remove ( " invalid-input" ) ;
84- }
88+ chatNickname . classList . remove ( ' invalid-input' ) ;
89+ } ;
8590}
0 commit comments