From 00b9a3d522d05830650f026ceceac75511618610 Mon Sep 17 00:00:00 2001 From: paras madan Date: Sun, 4 Aug 2019 17:37:59 +0530 Subject: [PATCH] geolocation enabled and tested --- public/index.html | 1 + public/js/index.js | 30 +++++++++++++++++++++++++++++- server/server.js | 12 ++++++------ server/utils/message.js | 10 +++++++++- server/utils/message.test.js | 15 ++++++++++++++- 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/public/index.html b/public/index.html index 3386c89..a575dc1 100644 --- a/public/index.html +++ b/public/index.html @@ -16,6 +16,7 @@ + diff --git a/public/js/index.js b/public/js/index.js index 2d841e7..fc75679 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -13,11 +13,22 @@ socket.on('disconnect',function(){ socket.on('newMessage',function(message){ // console.log("New Message",message); - var li = jQuery('
  • '); + var li = jQuery('
  • '); li.text(`${message.from}: ${message.text}`) jQuery('#messages').append(li); }) +socket.on('newLocationMessage',function(message){ + var li = jQuery('
  • '); + var a = jQuery('My current location') + 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", @@ -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'); + }) }) \ No newline at end of file diff --git a/server/server.js b/server/server.js index b40f3df..83525f4 100644 --- a/server/server.js +++ b/server/server.js @@ -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(); @@ -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",()=>{ diff --git a/server/utils/message.js b/server/utils/message.js index 7b0795e..777584c 100644 --- a/server/utils/message.js +++ b/server/utils/message.js @@ -6,4 +6,12 @@ var generateMessage = (from,text) =>{ } } -module.exports = {generateMessage}; \ No newline at end of file +var generateLocationMessage = (from,latitude,longitude)=>{ + return { + from, + url:`https://www.google.com/maps?q=${latitude},${longitude}`, + createdAt:new Date().getTime() +} +} + +module.exports = {generateMessage,generateLocationMessage}; \ No newline at end of file diff --git a/server/utils/message.test.js b/server/utils/message.test.js index 29fb1e3..408121a 100644 --- a/server/utils/message.test.js +++ b/server/utils/message.test.js @@ -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'; @@ -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}); + }) }) \ No newline at end of file