Skip to content

Commit

Permalink
Connection with username, working on #4.
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-fp committed Mar 5, 2017
1 parent d48ce09 commit adb8913
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
31 changes: 31 additions & 0 deletions client/app/chat_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";

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

let username = "";

module.exports = (io_client, server_url) => {

module.define_username = function() {
do {
username = readlineSync.question("Define username: ");
} while (!username);
};

module.connect_to_server = function(){
console.log("Attempting connection as ", username);

const socket = io_client.connect(server_url, { query: "username=" + username } );

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

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

return module;
};
17 changes: 8 additions & 9 deletions client/client.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"use strict";

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

const serverURL = 'http://localhost:9090';
const socket = ioClient.connect(serverURL, {query: 'username=marco'} );
const server_url = 'http://localhost:9090';

socket.on('connect', () => {
console.log("Connection established.");
});
console.log("Server url: ", server_url);

socket.on('server-msg', (response) => {
console.log("Received: ", response.data);
});
const chat_client = require('./app/chat_client.js')(io_client, server_url);

chat_client.define_username();

chat_client.connect_to_server();
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test": "test"
},
"dependencies": {
"readline-sync": "^1.4.6",
"socket.io": "*"
},
"devDependencies": {},
Expand Down
5 changes: 3 additions & 2 deletions server/app/chat.js → server/app/chat_server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"user strict";
"use strict";

module.exports = (io) => {

Expand All @@ -11,7 +11,8 @@ module.exports = (io) => {
socket.emit('server-msg', { data: "Welcome to Synapse, " + username + "."});

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

socket.on('disconnect', () => {
Expand Down
4 changes: 2 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);

const chat = require('./app/chat.js')(io);
const chat_server = require('./app/chat_server.js')(io);

app.use(express.static('public'));

Expand All @@ -14,7 +14,7 @@ app.get('/', (req, res) => {
});

io.on('connection', (socket) => {
chat.handle_client_connection(socket);
chat_server.handle_client_connection(socket);
});

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

0 comments on commit adb8913

Please sign in to comment.