Skip to content
Open

done #247

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"cors": "^2.8.5"
}
}
99 changes: 99 additions & 0 deletions week7/abby_li/history.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<!DOCTYPE html>
<html>
<head>
<title>Chat History</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #333;
text-align: center;
}
.message {
padding: 10px 15px;
margin-bottom: 10px;
border-radius: 5px;
background-color: #f1f1f1;
}
.message-content {
font-size: 16px;
margin-bottom: 5px;
}
.message-time {
font-size: 12px;
color: #777;
text-align: right;
}
.back-link {
display: block;
text-align: center;
margin-top: 20px;
color: #0066cc;
text-decoration: none;
}
.back-link:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>Chat History</h1>
<div id="message-container"></div>
<a href="/" class="back-link">Back to Chat</a>

<script>
// Function to format the date
function formatDate(dateString) {
const date = new Date(dateString);
return date.toLocaleString();
}

// Fetch messages from the API
fetch('/messages')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(messages => {
const container = document.getElementById('message-container');

if (messages.length === 0) {
container.innerHTML = '<p>No messages yet.</p>';
return;
}

// Sort messages by timestamp (newest first)
messages.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));

// Create HTML for each message
messages.forEach(message => {
const messageDiv = document.createElement('div');
messageDiv.className = 'message';

const contentP = document.createElement('p');
contentP.className = 'message-content';
contentP.textContent = message.content;

const timeP = document.createElement('p');
timeP.className = 'message-time';
timeP.textContent = formatDate(message.timestamp);

messageDiv.appendChild(contentP);
messageDiv.appendChild(timeP);
container.appendChild(messageDiv);
});
})
.catch(error => {
console.error('Error fetching messages:', error);
document.getElementById('message-container').innerHTML =
'<p>Error loading messages. Please try again later.</p>';
});
</script>
</body>
</html>
102 changes: 102 additions & 0 deletions week7/abby_li/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<!DOCTYPE html>
<html>
<head>
<title>Socket.IO Chat</title>
<style>
body {
margin: 0;
padding-bottom: 3rem;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

#form {
background: rgba(0, 0, 0, 0.15);
padding: 0.25rem;
position: fixed;
bottom: 0;
left: 0;
right: 0;
display: flex;
height: 3rem;
box-sizing: border-box;
backdrop-filter: blur(10px);
}

#input {
border: none;
padding: 0 1rem;
flex-grow: 1;
border-radius: 2rem;
margin: 0.25rem;
}

#input:focus {
outline: none;
}

#form > button {
background: #333;
border: none;
padding: 0 1rem;
margin: 0.25rem;
border-radius: 3px;
outline: none;
color: #fff;
cursor: pointer;
}

#messages {
list-style-type: none;
margin: 0;
padding: 0;
}

#messages > li {
padding: 0.5rem 1rem;
}

#messages > li:nth-child(odd) {
background: #efefef;
}
</style>
</head>
<body>
<ul id="messages"></ul>
<form id="form" action="">
<input id="input" autocomplete="off" placeholder="Type a message..." /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var form = document.getElementById('form');
var input = document.getElementById('input');
var messages = document.getElementById('messages');

// Load history messages
socket.on('load messages', function(msgs) {
msgs.forEach(function(msg) {
appendMessage(msg.content);
});
});

form.addEventListener('submit', function(e) {
e.preventDefault();
if (input.value) {
socket.emit('chat message', input.value);
input.value = '';
}
});

socket.on('chat message', function(msg) {
appendMessage(msg);
});

function appendMessage(msg) {
var item = document.createElement('li');
item.textContent = msg;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
}
</script>
</body>
</html>
64 changes: 64 additions & 0 deletions week7/abby_li/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const mongoose = require("mongoose");
const cors = require('cors');

// Enable CORS for all routes
app.use(cors());

// Serve static files
app.use(express.static(__dirname));

const Schema = mongoose.Schema;

const messageSchema = new Schema({
content: { type: String },
timestamp: { type: Date, default: Date.now }
})

const messageModel = mongoose.model("Message", messageSchema)

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

// Route for chat history page
app.get('/history', (req, res) => {
res.sendFile(__dirname + '/history.html');
});

app.get('/messages', async function(req, res){
res.json(await messageModel.find());
});

io.on('connection', function(socket){
console.log('User connected');

// Send history messages
messageModel.find().sort({ timestamp: -1 }).limit(10).then(messages => {
socket.emit('load messages', messages.reverse());
}).catch(error => {
console.error('Error loading messages:', error);
});

socket.on('chat message', function(msg){
const message = new messageModel();
message.content = msg;
message.save().then(m => {
io.emit('chat message', msg);
})
});

socket.on('disconnect', () => {
console.log('User disconnected');
});
});

server.listen(3000, async function(){
await mongoose.connect("mongodb+srv://sam:[email protected]/?retryWrites=true&w=majority&appName=Cluster0")
console.log('listening on *:3000');
});
11 changes: 11 additions & 0 deletions week7/abby_li/models/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const messageSchema = new Schema({
content: { type: String },
timestamp: { type: Date, default: Date.now }
});

const Message = mongoose.model('Message', messageSchema);

module.exports = Message;
Loading