Skip to content

Commit

Permalink
geolocation enabled and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
paras151 committed Aug 4, 2019
1 parent 5b8492b commit 00b9a3d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<button>Send</button>
</form>

<button id="send-location">Send location</button>
<script src = "/socket.io/socket.io.js"></script>
<script src = "js/libs/jquery-3.4.1.min.js"></script>
<script src = "js/index.js"></script>
Expand Down
30 changes: 29 additions & 1 deletion public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ socket.on('disconnect',function(){
socket.on('newMessage',function(message){
// console.log("New Message",message);

var li = jQuery('<li>');
var li = jQuery('<li></li>');
li.text(`${message.from}: ${message.text}`)
jQuery('#messages').append(li);
})

socket.on('newLocationMessage',function(message){
var li = jQuery('<li></li>');
var a = jQuery('<a target="_blank">My current location</a>')
li.text(`${message.from}: `);
a.attr('href',message.url);
li.append(a);
jQuery('#messages').append(li);
})



// socket.emit('createMessage',{
// from:"Erik",
// text:"Erik is all fit and fine",
Expand All @@ -35,4 +46,21 @@ jQuery('#message-form').on('submit',function(e){
},function(){

})
})

var locationButton = jQuery('#send-location');

locationButton.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 fetch location');
})
})
12 changes: 6 additions & 6 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path');
const http = require('http');
const express = require('express');
const socketIO = require('socket.io');
const {generateMessage} = require('./utils/message')
const {generateMessage,generateLocationMessage} = require('./utils/message')
const publicPath = path.join(__dirname,'../public');

const app = express();
Expand All @@ -24,11 +24,11 @@ io.on('connection',(socket)=>{

io.emit('newMessage',generateMessage(message.from,message.text))
callback('This is form server');
// socket.broadcast.emit('newMessage',{
// from:message.from,
// text:message.text,
// createdAt:new Date().getTime()
// })

})

socket.on('createLocationMessage',(coords)=>{
io.emit('newLocationMessage',generateLocationMessage('Admin',coords.latitude,coords.longitude))
})

socket.on("disconnect",()=>{
Expand Down
10 changes: 9 additions & 1 deletion server/utils/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ var generateMessage = (from,text) =>{
}
}

module.exports = {generateMessage};
var generateLocationMessage = (from,latitude,longitude)=>{
return {
from,
url:`https://www.google.com/maps?q=${latitude},${longitude}`,
createdAt:new Date().getTime()
}
}

module.exports = {generateMessage,generateLocationMessage};
15 changes: 14 additions & 1 deletion server/utils/message.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var expect = require('expect');

var {generateMessage} = require('./message')
var {generateMessage, generateLocationMessage} = require('./message')
describe('generateMessage',()=>{
it('should generate correct message object',()=>{
var from = 'Jen';
Expand All @@ -10,4 +10,17 @@ describe('generateMessage',()=>{
expect(typeof message.createdAt).toBe('number');
expect(message).toMatchObject({from, text});
})
})

describe('generateLocationMessage',()=>{
it('should generate correct location object',()=>{
var from = 'Monty';
var latitude = 15;
var longitude = 19;
var url = 'https://www.google.com/maps?q=15,19';
var message = generateLocationMessage(from, latitude,longitude);

// expect(typeof message.createdAt).toBe('number');
expect(message).toMatchObject({from, url});
})
})

0 comments on commit 00b9a3d

Please sign in to comment.