-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic chat on computer done, with user system and README showing used…
… libraries.
- Loading branch information
1 parent
626f6c6
commit 9cff2b2
Showing
26 changed files
with
1,044 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function key() { | ||
return $('#key').val(); | ||
} | ||
|
||
$(function () { | ||
var roomID = parseInt(location.pathname.match(/\d+/)[0]); | ||
$('#send').click(function () { | ||
if ($('#messageInput').val()) { | ||
$.ajax({ | ||
type: 'POST', | ||
url: '/chat/rooms/' + roomID + '/messages/add', | ||
data: { | ||
text: $('#messageInput').val(), | ||
key: key() | ||
} | ||
}).done(function () { | ||
$('#messageInput').val('') | ||
}); | ||
} | ||
}); | ||
|
||
$('#messageInput').keydown('ctrl+enter', function (e) { | ||
$('#send').click(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
function addMessage(sender, senderImg, content, messageID, isOut) { | ||
$('.mainChat').append( | ||
$('<div class="messageWrap">').append( | ||
$('<div class="userCard">').append( | ||
$('<img width="32" height="32">').attr('src', senderImg) | ||
).append( | ||
$('<a>').text(sender).attr('href', '/users/' + sender) | ||
) | ||
).append( | ||
$('<div class="messageBubble' + (isOut ? ' messageOut' : '') + '">').attr('id', messageID).html(markdown(content)) | ||
).append($('<hr>')) | ||
) | ||
} | ||
|
||
$(function () { | ||
window.CHAT = { | ||
room: { | ||
id: parseInt(location.pathname.match(/\d+/)[0]) | ||
}, user: { | ||
name: $('#usernameTitle').text().split(/Logged in as ([\w+ ]+)+/)[1] | ||
} | ||
}; | ||
|
||
function createChatWS() { | ||
var ws = new WebSocket('ws://' + location.hostname + ':4000/rooms/' + CHAT.room.id); | ||
ws.onmessage = function (msg) { | ||
var data = JSON.parse(msg.data); | ||
addMessage(data.senderName, data.senderImg, data.content, data.messageID, (data.senderName === CHAT.user.name)) | ||
$('.mainChat').scrollTop(99999999999999999); | ||
}; | ||
ws.onerror = function (event) { | ||
ws = createChatWS(); | ||
}; | ||
return ws; | ||
} | ||
|
||
CHAT.ws = createChatWS(); | ||
$.ajax({ | ||
type: 'POST', | ||
url: '/chat/rooms/' + CHAT.room.id + '/messages' | ||
}).done(function (data) { | ||
var messages = JSON.parse(data); | ||
for (var messageID in messages) { | ||
if (!messages.hasOwnProperty(messageID)) | ||
continue; | ||
var message = messages[messageID]; | ||
addMessage(message.senderName, message.senderImg, message.content, messageID, (message.senderName === CHAT.user.name)); | ||
} | ||
$('.mainChat').scrollTop(999); | ||
$('#blockChat, #chatLoading').remove(); | ||
}); | ||
}); |
Oops, something went wrong.