-
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.
- Loading branch information
0 parents
commit 80aad75
Showing
8 changed files
with
172 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
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,21 @@ | ||
{ | ||
"name": "node-chat-app", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node server/server.js", | ||
"test": "mocha server/**/*.test.js", | ||
"test-watch": "nodemon --exec 'npm test'" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.15.3", | ||
"socket.io": "^2.0.3" | ||
}, | ||
"devDependencies": { | ||
"expect": "^1.20.2", | ||
"mocha": "^3.4.2" | ||
} | ||
} |
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,22 @@ | ||
<!DOCTYPE html> | ||
|
||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
</head> | ||
|
||
<body> | ||
<p>Welcome to the chate app </p> | ||
|
||
<ol id="messages"></ol> | ||
<form id="message-form"> | ||
<input name="message" type="text" placeholder="Message"/> | ||
<button>Send</button> | ||
</form> | ||
<button id="send-location">Send Location to Users</button> | ||
|
||
<script = src="socket.io/socket.io.js"> </script> | ||
<script = src="/js/libs/jquery-3.2.1.min.js"> </script> | ||
<script src="/js/index.js"> </script> | ||
</body> | ||
</html> |
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,54 @@ | ||
var socket = io(); | ||
|
||
socket.on('connect', function() { | ||
console.log('Connected to server'); | ||
|
||
}); | ||
|
||
socket.on('disconnect', function() { | ||
console.log('Disconnected from the server'); | ||
}); | ||
|
||
socket.on('newMessage', function(message){ | ||
var li = jQuery('<li></li>'); | ||
li.text(`${message.from}: ${message.text}`); | ||
|
||
jQuery('#messages').append(li); | ||
}); | ||
|
||
socket.emit('createMessage', { | ||
from: 'test', | ||
text: 'hi' | ||
}, function(data){ | ||
console.log(data); | ||
}); | ||
|
||
jQuery('#message-form').on('submit', function(e){ | ||
e.preventDefault(); | ||
|
||
socket.emit('createMessage', { | ||
from: 'User', | ||
text: jQuery('[name=message]').val() | ||
}, function(){ | ||
|
||
}); | ||
}); | ||
|
||
var locationButton = jQuery('#send-location'); | ||
locationButtion.on('click', function(){ | ||
if(!navigator.geolocation){ | ||
return alert('Geolocation not supported by your browser') | ||
} | ||
|
||
navigator.geolocation.getCurrentPosition(function(position){ | ||
socket.emit('createLocationMessage', { | ||
latitude: position.coords.latitude, | ||
longitude: position.coords.longitude | ||
}); | ||
}, function(){ | ||
alert('Unable to get location') | ||
}); | ||
}); | ||
|
||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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,44 @@ | ||
const path = require('path'); | ||
const http = require('http'); | ||
const express = require('express'); | ||
const socketIO = require('socket.io'); | ||
|
||
const {generateMessage} = require ('./utils/message'); | ||
const publicPath = path.join(__dirname, '../public'); | ||
const port = process.env.PORT || 3000; | ||
var app = express(); | ||
var server = http.createServer(app); | ||
var io = socketIO(server); | ||
|
||
app.use(express.static(public)); | ||
|
||
io.on('connection', (socket) => { | ||
|
||
|
||
socket.emit('newMessage', generateMessage('Admin', 'Welcome to the Chat app')); | ||
|
||
socket.broadcast.emit('newMessage', generateMessage('Admin', 'A new user has joined')); | ||
|
||
|
||
socket.on('createMessage', (message, callback) =>{ | ||
console.log('createMesse', message); | ||
io.emit('newMessage', generateMessage(message.from, message.text)); | ||
callback('This is from the server'); | ||
}); | ||
|
||
socket.on('createLocationMessage', (coords) => { | ||
io.emit('newMessage', generateMessage('Admin', `${coords.latitude}, ${coords.logitude}`)) | ||
}); | ||
|
||
socket.on('disconnect', () =>{ | ||
console.log('User was disconnected'); | ||
}); | ||
|
||
}); | ||
|
||
|
||
|
||
server.listen(port,() =>{ | ||
console.log('server is up on port 3000'); | ||
}); | ||
|
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,9 @@ | ||
var generateMessage = (from, text) => { | ||
return { | ||
from, | ||
text, | ||
createdAt: new Date().getTime() | ||
} | ||
} | ||
|
||
module.exports = {generateMessage}; |
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,17 @@ | ||
var expect = require('expect'); | ||
var {generateMessage} = require ('./message'); | ||
|
||
describe('generateMessage', () => { | ||
it('should genearte the correct message object', () => { | ||
var from = 'Test'; | ||
var text = 'Text message'; | ||
var message = generateMessage(from, text); | ||
|
||
expect(message.createdAt).toBeA('number'); | ||
expect(message).toInclude({ | ||
from, | ||
text | ||
}); | ||
|
||
}) | ||
}); |