11import { getCookie } from './assets.js'
22import * as app_states from './app_states.js'
33
4+ let ws
5+
6+ function connectWebSocket ( ) {
7+ // Create a new WebSocket connection to the Tornado server with user ID
8+ const userId = app_states . userId
9+
10+ // Create WebSocket connection
11+ const wsHost = env_var . TORNADO_HOSTNAME
12+ ws = new WebSocket ( `${ wsHost } /ws/chat?user_id=${ userId } ` )
13+
14+ ws . onopen = function ( ) {
15+ console . log ( 'WebSocket connection opened' )
16+ }
17+
18+ ws . onmessage = function ( event ) {
19+ // Handle incoming messages from the WebSocket server
20+ const message = JSON . parse ( event . data )
21+ console . log ( 'Received message:' , message )
22+ // Update the chat UI with the new message
23+ if (
24+ message . sender . toString ( ) === app_states . selectedContactId . toString ( ) &&
25+ message . receiver . toString ( ) === app_states . userId . toString ( )
26+ ) {
27+ console . log ( 'Received message:' , message )
28+ app_states . messages . push ( message )
29+ rerender_msg_view ( )
30+ }
31+ }
32+
33+ ws . onclose = function ( ) {
34+ console . log ( 'WebSocket connection closed' )
35+ }
36+
37+ ws . onerror = function ( error ) {
38+ console . log ( 'WebSocket error:' , error )
39+ }
40+ }
41+
42+ function sendMessageToWebSocket ( message ) {
43+ // Send a message to the WebSocket server
44+ if ( ws && ws . readyState === WebSocket . OPEN ) {
45+ ws . send ( JSON . stringify ( message ) )
46+ } else {
47+ console . log ( 'WebSocket is not open' )
48+ }
49+ }
50+
451function rerender_msg_view ( ) {
52+ // rerender the mes_view using the app_state message list
53+ const msgList = app_states . messages
54+ const chatBody = $ ( '.chat-body' )
55+ chatBody . empty ( )
56+ msgList . forEach ( ( message ) => {
57+ const messageElement = $ ( '<div>' ) . addClass (
58+ message . sender === app_states . userId
59+ ? 'chat-message chat-message-sent'
60+ : 'chat-message chat-message-received'
61+ )
62+ const profile = $ ( '<div>' ) . addClass ( 'chat-message-profile' )
63+ const content = $ ( '<div>' ) . addClass ( 'chat-message-content' )
64+ content . append ( $ ( '<p>' ) . text ( message . content ) )
65+ messageElement . append ( profile , content )
66+ chatBody . append ( messageElement )
67+ } )
68+ // scroll to bottom
69+ $ ( '.chat-body' ) . scrollTop ( $ ( '.chat-body' ) [ 0 ] . scrollHeight )
70+ }
71+
72+ function render_msg_view ( ) {
573 // reads the app_state and rerenders the message view
674
775 // check if user is logged in
@@ -41,22 +109,7 @@ function rerender_msg_view() {
41109 console . log ( 'Messages:' , response )
42110 app_states . setMessages ( response )
43111 // render the messages
44- const chatBody = $ ( '.chat-body' )
45- chatBody . empty ( )
46- response . forEach ( ( message ) => {
47- const messageElement = $ ( '<div>' ) . addClass (
48- message . sender === app_states . userId
49- ? 'chat-message chat-message-sent'
50- : 'chat-message chat-message-received'
51- )
52- const profile = $ ( '<div>' ) . addClass ( 'chat-message-profile' )
53- const content = $ ( '<div>' ) . addClass ( 'chat-message-content' )
54- content . append ( $ ( '<p>' ) . text ( message . content ) )
55- messageElement . append ( profile , content )
56- chatBody . append ( messageElement )
57- // scroll to bottom
58- chatBody . scrollTop ( chatBody [ 0 ] . scrollHeight )
59- } )
112+ rerender_msg_view ( )
60113 } ,
61114 error : function ( response ) {
62115 console . log ( 'Error:' , response )
@@ -115,7 +168,7 @@ function onClickContact() {
115168 // on click of a contact, set the selectedContactId and rerender the message view
116169 const contactId = $ ( this ) . attr ( 'id' )
117170 app_states . setSelectedContactId ( contactId )
118- rerender_msg_view ( )
171+ render_msg_view ( )
119172}
120173
121174function onClickSend ( ) {
@@ -135,11 +188,16 @@ function onClickSend() {
135188 } ,
136189 success : function ( response ) {
137190 console . log ( 'Message sent:' , response )
191+ // Create a message object and send it to the WebSocket server
192+ const wsMessage = {
193+ sender : app_states . userId ,
194+ receiver : app_states . selectedContactId ,
195+ content : message ,
196+ }
197+ sendMessageToWebSocket ( wsMessage )
138198 app_states . messages . push ( response )
139- rerender_msg_view ( )
199+ render_msg_view ( )
140200 $ ( '#message-box' ) . val ( '' )
141- // scroll to bottom
142- $ ( '.chat-body' ) . scrollTop ( $ ( '.chat-body' ) [ 0 ] . scrollHeight )
143201 } ,
144202 error : function ( response ) {
145203 console . log ( 'Error:' , response )
@@ -271,6 +329,8 @@ $(function () {
271329
272330 rerender_contacts_view ( )
273331
332+ connectWebSocket ( )
333+
274334 $ ( '#chat-msg-send-btn' ) . on ( 'click' , onClickSend )
275335
276336 $ ( '#new-message-btn' ) . click ( function ( ) {
0 commit comments