blob: 931f598357874f011f8f18d6e330a3c230a38503 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
// TODO: Handle disconnect
/**
* Handles ingame networking;
*/
export default class GameClient {
/**
* Defines basic attributes
* @param {string} user The username of the player
* @param {HubConnection} connection Already established connection to the
* server
*/
constructor(user, connection) {
this.user = user;
this.connection = connection;
}
/**
* Registers chat html component
* @param {string} chatId Id of chat component
*/
registerChat(chatId) {
this.chat = document.getElementById(chatId);
this.messageList = this.chat.querySelector('#message-list');
this.messageInput = this.chat.querySelector('#input-message');
this.messageSend = this.chat.querySelector('#send-message');
this.connection.on('ReceiveMessage', (user, message) => {
let msg = message.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
let encodedMsg = user + ' sagt: ' + msg;
let messageP = document.createElement('p');
messageP.class = 'message';
messageP.textContent = encodedMsg;
this.messageList.appendChild(messageP);
});
this.messageSend.addEventListener('click', () => {
let message = this.messageInput.value;
this.connection.invoke('SendMessage', this.user, message);
});
}
}
|