Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ddd419

Browse files
author
Andrew Mead
committed
Lesson: Timestamps for Location Messages
1 parent 96d6479 commit 9ddd419

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

chat-app/public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<script id="location-message-template" type="text/html">
2424
<div>
25-
<p><a href="{{url}}" target="_blank">My current location</a></p>
25+
<p>{{createdAt}} - <a href="{{url}}" target="_blank">My current location</a></p>
2626
</div>
2727
</script>
2828

chat-app/public/js/chat.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ socket.on('message', (message) => {
2020
$messages.insertAdjacentHTML('beforeend', html)
2121
})
2222

23-
socket.on('locationMessage', (url) => {
24-
console.log(url)
23+
socket.on('locationMessage', (message) => {
24+
console.log(message)
2525
const html = Mustache.render(locationMessageTemplate, {
26-
url
26+
url: message.url,
27+
createdAt: moment(message.createdAt).format('h:mm a')
2728
})
2829
$messages.insertAdjacentHTML('beforeend', html)
2930
})

chat-app/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const http = require('http')
33
const express = require('express')
44
const socketio = require('socket.io')
55
const Filter = require('bad-words')
6-
const { generateMessage } = require('./utils/messages')
6+
const { generateMessage, generateLocationMessage } = require('./utils/messages')
77

88
const app = express()
99
const server = http.createServer(app)
@@ -32,7 +32,7 @@ io.on('connection', (socket) => {
3232
})
3333

3434
socket.on('sendLocation', (coords, callback) => {
35-
io.emit('locationMessage', `https://google.com/maps?q=${coords.latitude},${coords.longitude}`)
35+
io.emit('locationMessage', generateLocationMessage(`https://google.com/maps?q=${coords.latitude},${coords.longitude}`))
3636
callback()
3737
})
3838

chat-app/src/utils/messages.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ const generateMessage = (text) => {
55
}
66
}
77

8+
const generateLocationMessage = (url) => {
9+
return {
10+
url,
11+
createdAt: new Date().getTime()
12+
}
13+
}
14+
815
module.exports = {
9-
generateMessage
16+
generateMessage,
17+
generateLocationMessage
1018
}

0 commit comments

Comments
 (0)