11from flask import Flask , request , session
22from flask_socketio import SocketIO , emit , join_room
3- # from elasticsearch import Elasticsearch
4- # from elasticsearch import helpers
3+ from elasticsearch import Elasticsearch
4+ from elasticsearch import helpers
55import datetime
66import redis
77import requests
@@ -43,18 +43,19 @@ class Settings(BaseSettings):
4343
4444
4545### elk, kibana
46- # es = Elasticsearch(f'{ES_IP}:{ES_PORT}') ## 변경
47- # es.info()
46+ es = Elasticsearch (f'{ ES_IP } :{ ES_PORT } ' ) ## 변경
47+ es .info ()
4848
49- # def utc_time():
50- # return datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
5149
52- # def make_index(es, index_name):
53- # if es.indices.exists(index=index_name):
54- # es.indices.delete(index=index_name)
55- # es.indices.create(index=index_name)
50+ def utc_time ():
51+ return datetime .datetime .utcnow ().strftime ('%Y-%m-%dT%H:%M:%S.%f' )[:- 3 ] + 'Z'
5652
57- # index_name= 'webrtc_room'
53+ def make_index (es , index_name ):
54+ if es .indices .exists (index = index_name ):
55+ es .indices .delete (index = index_name )
56+ es .indices .create (index = index_name )
57+
58+ index_name = 'webrtc_room'
5859
5960@app .route ('/' )
6061def hello ():
@@ -79,12 +80,12 @@ def on_create_room(data):
7980 emit ("join-request" )
8081
8182
82- # elk
83- # room_id = data["room_id"]
84- # date = datetime.datetime.now()
85- # now = date.strftime('%m/%d/%y %H:%M:%S')
86- # doc_create= {"des":"create room", "room_id":room_id, "@timestamp": utc_time()}
87- # es.index(index=index_name, doc_type="log", body=doc_create)
83+ # elk
84+ room_id = data ["room_id" ]
85+ date = datetime .datetime .now ()
86+ now = date .strftime ('%m/%d/%y %H:%M:%S' )
87+ doc_create = {"des" :"create room" , "room_id" :room_id , "@timestamp" : utc_time ()}
88+ es .index (index = index_name , doc_type = "log" , body = doc_create )
8889
8990
9091@socketio .on ("join-room" )
@@ -111,10 +112,10 @@ def on_join_room(data):
111112 print ("[{}] New member joined: {}<{}>" .format (room_id , display_name , sid ))
112113
113114 ### elk
114- # date = datetime.datetime.now()
115- # now = date.strftime('%m/%d/%y %H:%M:%S')
116- # doc_join= {"des":"New member joined", "room_id":room_id, "sid": sid, "@timestamp": utc_time()}
117- # es.index(index=index_name, doc_type="log", body=doc_join)
115+ date = datetime .datetime .now ()
116+ now = date .strftime ('%m/%d/%y %H:%M:%S' )
117+ doc_join = {"des" :"New member joined" , "room_id" :room_id , "sid" : sid , "@timestamp" : utc_time ()}
118+ es .index (index = index_name , doc_type = "log" , body = doc_join )
118119 emit ("user-connect" , {"sid" : sid , "name" : display_name }, broadcast = True , include_self = False , room = room_id )
119120
120121 message = {
@@ -166,16 +167,16 @@ def on_disconnect():
166167 display_name = names_sid [sid ]
167168
168169 ### elk
169- # now = datetime.datetime.now()
170- # now = now.strftime('%m/%d/%y %H:%M:%S')
171- # doc_disconnect= {"des":"user-disconnect", "room_id":room_id, "sid": sid, "@timestamp": utc_time()}
172- # es.index(index=index_name, doc_type="log", body=doc_disconnect)
170+ now = datetime .datetime .now ()
171+ now = now .strftime ('%m/%d/%y %H:%M:%S' )
172+ doc_disconnect = {"des" :"user-disconnect" , "room_id" :room_id , "sid" : sid , "@timestamp" : utc_time ()}
173+ es .index (index = index_name , doc_type = "log" , body = doc_disconnect )
173174
174175 print ("[{}] Member left: {}<{}>" .format (room_id , display_name , sid ))
175176 message = {
176177 "sid" : sid ,
177178 "name" : display_name ,
178- 'type' : "join "
179+ 'type' : "disconnect "
179180 }
180181 emit ("chatting" , message , broadcast = True , include_self = True , room = room_id )
181182
@@ -215,16 +216,26 @@ def send_message(message):
215216 room_id = message ["room_id" ]
216217
217218 ### elk
219+
218220 # date = datetime.datetime.now()
219221 # now = date.strftime('%m/%d/%y %H:%M:%S')
220222 # doc_chatting= {"des" : "chatting", "room_id" : room_id, "chatting message" : text,"@timestamp": utc_time()}
221223 # es.index(index=index_name, doc_type="log", body=doc_chatting)
222224
225+
226+ date = datetime .datetime .now ()
227+ now = date .strftime ('%m/%d/%y %H:%M:%S' )
228+ doc_chatting = {"des" : "chatting" , "room_id" : room_id , "chatting message" : text ,"@timestamp" : utc_time ()}
229+ es .index (index = index_name , doc_type = "log" , body = doc_chatting )
230+
231+
223232 data = {
224233 "text" : text ,
225234 "room_id" : room_id ,
226235 "sender" : sender ,
227- "type" : "normal"
236+ "type" : "normal" ,
237+ "direct" : False , # react에서 dm인지 아닌지 확인할 수 있는 필드
238+ "target" : "self"
228239 }
229240
230241 # front로부터 받은 data에 direct라는 필드가 있고 false 값이라면 브로드캐스팅을 하고
@@ -233,7 +244,11 @@ def send_message(message):
233244 if message ["direct" ] == False :
234245 emit ("chatting" , data , broadcast = True , include_self = True , room = room_id )
235246 else :
247+ data ["direct" ] = True
248+ emit ("chatting" , data , to = request .sid )
249+ data ["target" ] = "other"
236250 emit ("chatting" , data , to = message ["dest" ])
251+
237252 # broadcast to others in the room
238253 # emit("chatting", data, room=room_id)
239254
@@ -281,4 +296,4 @@ def exit_room(socketID):
281296 host = "0.0.0.0" ,
282297 port = 5000
283298 )
284- # make_index(es, index_name)
299+ make_index (es , index_name )
0 commit comments