-
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.
- use repository in app - test with simple html
- Loading branch information
Showing
6 changed files
with
109 additions
and
39 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 |
---|---|---|
@@ -1,6 +1,23 @@ | ||
import pino from "pino"; | ||
import { ChatServer } from "./chat-server"; | ||
import CommandService from "./command-service"; | ||
import ChatRepo, { ChatSession } from "./repositories/chat-repo"; | ||
import { MongoClient, Collection } from "mongodb"; | ||
|
||
// config, consider to use env variables in the future | ||
const uri = process.env.MONGODB_URI || "mongodb://localhost:27017"; | ||
const dbName = "math-chat"; | ||
const collectionName = "chat-session"; | ||
|
||
const logger = pino(); | ||
const chatServer = new ChatServer(logger, 3000); | ||
const collection = getMongoCollection(uri, dbName, collectionName); | ||
const commandService = new CommandService(logger, new ChatRepo(collection)); | ||
const chatServer = new ChatServer(logger, commandService, 3000); | ||
chatServer.listen(); | ||
|
||
function getMongoCollection(uri: string, dbName: string, collectionName: string): Collection<ChatSession> { | ||
const mongoClient = new MongoClient(uri); | ||
const db = mongoClient.db(dbName); | ||
const collection = db.collection<ChatSession>(collectionName); | ||
return collection; | ||
} |
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
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
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
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
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 |
---|---|---|
@@ -1,46 +1,66 @@ | ||
<!-- https://github.com/socketio/socket.io-minimal-example/blob/main/index.html --> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>Socket.IO Fiddle</title> | ||
</head> | ||
|
||
<body> | ||
<h2>Status: <span id="status">Disconnected</span></h2> | ||
<h2>Messages:</h2> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Simple Chat</title> | ||
</head> | ||
<body> | ||
<ul id="messages"></ul> | ||
|
||
<form id="chat-form"> | ||
<input id="chat-input" type="text" autocomplete="off"> | ||
<button>Send</button> | ||
</form> | ||
<button id="history-button">History</button> | ||
<ul id="history"></ul> | ||
|
||
<script src="http://localhost:3000/socket.io/socket.io.js"></script> | ||
<script> | ||
const status = document.getElementById("status"); | ||
const messages = document.getElementById("messages"); | ||
|
||
const appendMessage = (content) => { | ||
const item = document.createElement("li"); | ||
item.textContent = content; | ||
messages.appendChild(item); | ||
}; | ||
<script > | ||
const socket = io('http://localhost:3000'); | ||
const form = document.getElementById('chat-form'); | ||
const input = document.getElementById('chat-input'); | ||
const messages = document.getElementById('messages'); | ||
|
||
const socket = io("http://localhost:3000"); | ||
|
||
socket.on("connect", () => { | ||
status.innerText = "Connected"; | ||
appendMessage(`event: connect | session id: ${socket.id}`); | ||
form.addEventListener('submit', (e) => { | ||
e.preventDefault(); | ||
if (input.value) { | ||
socket.emit('operation', input.value); | ||
appendMessage(`You: ${input.value}`, 'blue'); | ||
input.value = ''; | ||
} | ||
}); | ||
|
||
socket.on("connect_error", (err) => { | ||
appendMessage(`event: connect_error | reason: ${err.message}`); | ||
socket.on('result', (msg) => { | ||
appendMessage(`Bot: ${msg}`, 'green'); | ||
}); | ||
|
||
socket.on("disconnect", (reason) => { | ||
status.innerText = "Disconnected"; | ||
appendMessage(`event: disconnect | reason: ${reason}`); | ||
socket.on('error', (error) => { | ||
appendMessage(`Bot: [Error] ${error}`, 'red'); | ||
}); | ||
|
||
socket.onAny((event, ...args) => { | ||
appendMessage(`event: ${event} | arguments: ${args}`); | ||
function appendMessage(message, color) { | ||
const item = document.createElement('li'); | ||
item.textContent = message; | ||
item.style.color = color; | ||
messages.appendChild(item); | ||
window.scrollTo(0, document.body.scrollHeight); | ||
} | ||
|
||
const historyButton = document.getElementById('history-button'); | ||
|
||
historyButton.addEventListener('click', () => { | ||
socket.emit('history'); | ||
socket.on('history', (history) => { | ||
const historyList = document.getElementById('history'); | ||
historyList.innerHTML = ''; | ||
history.forEach(item => { | ||
const li = document.createElement('li'); | ||
li.textContent = `${item.expression} = ${item.result}`; | ||
historyList.appendChild(li); | ||
}); | ||
}); | ||
}); | ||
|
||
</script> | ||
</body> | ||
</html> | ||
</body> | ||
</html> |