mirror of
https://github.com/lucaspalomodevelop/Party.git
synced 2026-03-13 08:09:37 +00:00
sending messages works
This commit is contained in:
parent
758c1807f8
commit
6d392df7cb
@ -9,21 +9,22 @@ class Chat:
|
||||
@classmethod
|
||||
def init(self):
|
||||
self.CONNECTION_STRING = "mongodb://localhost:27017/"
|
||||
self.client = MongoClient(self.CONNECTION_STRING)['partyyy']
|
||||
self.collection = self.client['messages']
|
||||
self.client = MongoClient(self.CONNECTION_STRING)
|
||||
self.collection = self.client['partyyy']['messages']
|
||||
self.initialized = True
|
||||
|
||||
|
||||
@classmethod
|
||||
def getAllMessages(self)-> list[dict]:
|
||||
messages = self.collection.find().sort('timestamp', -1)
|
||||
print(messages)
|
||||
return list(messages)
|
||||
|
||||
@classmethod
|
||||
def insertMessage(self, content: str, author: str, timestamp: str):
|
||||
self.collection.insert_one(self.convertToMessage(content, author, timestamp))
|
||||
|
||||
message = self.convertToMessage(content, author, timestamp)
|
||||
print(message)
|
||||
self.collection.insert_one(message)
|
||||
print('inserted message')
|
||||
|
||||
@classmethod
|
||||
def getNextNMessages(self, count: int, skip: int) -> list[dict]:
|
||||
@ -36,9 +37,8 @@ class Chat:
|
||||
def convertToMessage(self, content: str, author: str, timestamp: str) -> dict:
|
||||
message = {
|
||||
'content': content,
|
||||
'author': author,
|
||||
'sender': author,
|
||||
'timestamp': timestamp,
|
||||
}
|
||||
print(message)
|
||||
|
||||
return message
|
||||
|
||||
@ -65,14 +65,15 @@ def api_mate_status():
|
||||
|
||||
@app.route('/api/chat/', methods=['GET'])
|
||||
def api_chat():
|
||||
messages = Chat.getNextNMessages(100, 0) # 0 offset print(messages)
|
||||
messages = Chat.getNextNMessages(100, 0) # 0 offset
|
||||
response = jsonify(success=True, messages=json_util.dumps(messages))
|
||||
response.status_code = 200
|
||||
return response
|
||||
|
||||
@socketio.on('chat-message')
|
||||
def handle_chat_message(data):
|
||||
Chat.insertMessage(data['content'], data['author'], data['timestamp'])
|
||||
print(data)
|
||||
Chat.insertMessage(data['text'], data['sender'], data['timestamp'])
|
||||
|
||||
@socketio.on('chat-get-messages')
|
||||
def handle_chat_get_message(data):
|
||||
@ -84,14 +85,11 @@ def handle_chat_get_message(data):
|
||||
@socketio.on('mate-status')
|
||||
def handle_mate_status():
|
||||
status = MateKiste.getStatus()
|
||||
print(status)
|
||||
return { 'data': status }
|
||||
|
||||
@socketio.on('mate-nehmen')
|
||||
def handle_mate_nehmen(data):
|
||||
print(data)
|
||||
MateKiste.removeAt(data['row'], data['column'])
|
||||
# broadcast
|
||||
emit('mate-genommen', data, broadcast=True)
|
||||
|
||||
@socketio.on('mate-reset')
|
||||
@ -102,12 +100,10 @@ def handle_mate_reset():
|
||||
|
||||
@socketio.on('connect')
|
||||
def handle_connect():
|
||||
print('Client connected')
|
||||
join_room(0)
|
||||
|
||||
@socketio.on('disconnect')
|
||||
def handle_disconnect():
|
||||
print('Client disconnected')
|
||||
leave_room(0)
|
||||
|
||||
@app.route('/api/music', methods=['GET'])
|
||||
|
||||
@ -51,3 +51,27 @@ function buildChatMessage(author, content, timestamp) {
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
function sendMessage() {
|
||||
const chatInput = document.getElementById('chatInputField');
|
||||
if (chatInput.value.length < 0) return;
|
||||
|
||||
const userName = window.sessionStorage.getItem('name');
|
||||
|
||||
const messageDate = new Date();
|
||||
|
||||
const message = { sender: userName, text: chatInput.value, timestamp: messageDate };
|
||||
|
||||
console.log(message);
|
||||
|
||||
socket.emit('chat-message', message);
|
||||
console.log('message sent');
|
||||
|
||||
chatInput.value = '';
|
||||
}
|
||||
|
||||
socket.on('chat-message', (data) => {
|
||||
const chatBox = document.getElementById('chatBox');
|
||||
chatBox.appendChild(buildChatMessage(data.sender, data.text, data.timestamp['$date'])); // $date is mongodb date
|
||||
chatBox.scrollTop = chatBox.scrollHeight;
|
||||
});
|
||||
|
||||
@ -85,6 +85,10 @@
|
||||
<h1>Chat</h1>
|
||||
<div id="chatBox">
|
||||
</div>
|
||||
<div id="chatInput">
|
||||
<input id="chatInputField" type="text" placeholder="Nachricht">
|
||||
<button id="chatInputButton" onclick="sendMessage()">Senden</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="hidden-div">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user