Skip to content

Commit

Permalink
Chat functionality. #4 and #2
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-fp committed Mar 6, 2017
1 parent adb8913 commit 5cde356
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
37 changes: 35 additions & 2 deletions client/app/chat_client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

const readlineSync = require('readline-sync');
const readline = require('readline');

let username = "";

Expand All @@ -19,13 +20,45 @@ module.exports = (io_client, server_url) => {

socket.on('connect', () => {
console.log("Connection established.");
//socket.emit('msg', 'placeholder');
});

socket.on('welcome-msg', (response) => {
console.log("<Server>: ", response.data);
start_chat(socket);
});

socket.on('server-msg', (response) => {
console.log("Received: ", response.data);
console.log("<Server>: ", response.data);
});

socket.on("client-msg", (response) => {
console.log("<" + response.username + ">: " + response.message);
});
};

return module;
};

function start_chat(socket){
console.log("Starting chat, write \".exit\" to stop.");
let continue_chat = true;
const readlineInterface = getReadlineInterface();
show_prompt(readlineInterface, socket);
}

function getReadlineInterface(){
return readline.createInterface({ input: process.stdin, output: process.stdout });
}

function show_prompt(readlineInterface, socket){
readlineInterface.question("$> ", (input) => {
switch(input){
case ".exit":
process.exit();
break;
default:
socket.emit("client-msg", {username: username, message: input});
show_prompt(readlineInterface, socket);
}
});
}
1 change: 0 additions & 1 deletion client/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

const repl = require('repl');
const io_client = require('socket.io-client');

const server_url = 'http://localhost:9090';
Expand Down
13 changes: 5 additions & 8 deletions server/app/chat_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

module.exports = (io) => {

module.handle_client_connection = function(socket) {

let username = socket.handshake.query.username
module.handle_client_connection = function(socket, username) {

console.log( "<" + username + "> entered the chat.");

socket.emit('server-msg', { data: "Welcome to Synapse, " + username + "."});
socket.emit('welcome-msg', { data: "Welcome to Synapse, " + username + "."});

socket.on('msg', (content) => {
//console.log('content: ', content);
socket.emit('server-msg', content);
socket.on('client-msg', (client_message) => {
socket.broadcast.emit('client-msg', client_message);
});

 
socket.on('disconnect', () => {
console.log('User disconnected');
});
Expand Down
7 changes: 6 additions & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ app.get('/', (req, res) => {
});

io.on('connection', (socket) => {
chat_server.handle_client_connection(socket);
let username = socket.handshake.query.username;
if(username){
chat_server.handle_client_connection(socket, username);
} else {
socket.disconnect(0);
}
});

http.listen(9090, () => {
Expand Down

0 comments on commit 5cde356

Please sign in to comment.