forked from noxious/server
added socket.io
This commit is contained in:
parent
95b4849200
commit
b2bfc77ca3
@ -1 +1,2 @@
|
||||
SERVER_PORT=3000
|
||||
#DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -308,3 +308,6 @@ $RECYCLE.BIN/
|
||||
*.lnk
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/node,jetbrains+all,visualstudiocode,macos,windows
|
||||
|
||||
prisma/dev.db
|
||||
prisma/dev.db-journal
|
1364
package-lock.json
generated
1364
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
13
package.json
13
package.json
@ -2,15 +2,20 @@
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js",
|
||||
"dev": "ts-node-dev --respawn --transpile-only src/server.ts"
|
||||
"dev": "nodemon src/server.ts --exec ts-node"
|
||||
},
|
||||
"dependencies": {
|
||||
"ws": "^8.17.0"
|
||||
"@prisma/client": "^5.13.0",
|
||||
"express": "^4.19.2",
|
||||
"prisma": "^5.13.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/ws": "^8.5.10",
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/node": "^20.12.8",
|
||||
"@types/socket.io": "^3.0.2",
|
||||
"dotenv": "^16.4.5",
|
||||
"ts-node-dev": "^2.0.0",
|
||||
"nodemon": "^3.1.0",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^5.4.5"
|
||||
}
|
||||
}
|
||||
|
9
prisma/migrations/20240502213424_init/migration.sql
Normal file
9
prisma/migrations/20240502213424_init/migration.sql
Normal file
@ -0,0 +1,9 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "User" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"email" TEXT NOT NULL,
|
||||
"name" TEXT
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
|
@ -0,0 +1,22 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the column `email` on the `User` table. All the data in the column will be lost.
|
||||
- You are about to drop the column `name` on the `User` table. All the data in the column will be lost.
|
||||
- Added the required column `password` to the `User` table without a default value. This is not possible if the table is not empty.
|
||||
- Added the required column `username` to the `User` table without a default value. This is not possible if the table is not empty.
|
||||
|
||||
*/
|
||||
-- RedefineTables
|
||||
PRAGMA foreign_keys=OFF;
|
||||
CREATE TABLE "new_User" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"username" TEXT NOT NULL,
|
||||
"password" TEXT NOT NULL
|
||||
);
|
||||
INSERT INTO "new_User" ("id") SELECT "id" FROM "User";
|
||||
DROP TABLE "User";
|
||||
ALTER TABLE "new_User" RENAME TO "User";
|
||||
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");
|
||||
PRAGMA foreign_key_check;
|
||||
PRAGMA foreign_keys=ON;
|
3
prisma/migrations/migration_lock.toml
Normal file
3
prisma/migrations/migration_lock.toml
Normal file
@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "sqlite"
|
20
prisma/schema.prisma
Normal file
20
prisma/schema.prisma
Normal file
@ -0,0 +1,20 @@
|
||||
// This is your Prisma schema file,
|
||||
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
||||
|
||||
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
|
||||
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = "file:./dev.db"
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
username String @unique
|
||||
password String
|
||||
}
|
@ -1,24 +1,31 @@
|
||||
import * as dotenv from 'dotenv';
|
||||
import express from 'express';
|
||||
import http from 'http';
|
||||
import { Server } from 'socket.io';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
import WebSocket from 'ws';
|
||||
import * as process from "node:process";
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
const io = new Server(server, {
|
||||
cors: {
|
||||
origin: '*',
|
||||
}
|
||||
});
|
||||
|
||||
const wss = new WebSocket.Server({ port: process.env.SERVER_PORT });
|
||||
app.get('/', (req: express.Request, res: express.Response) => {
|
||||
res.send('<h1>Hey Socket.io</h1>');
|
||||
});
|
||||
|
||||
console.log(`Server started on port ${process.env.SERVER_PORT}`);
|
||||
|
||||
wss.on('connection', (ws: WebSocket) => {
|
||||
console.log('New client connected');
|
||||
|
||||
ws.on('message', (message: string) => {
|
||||
console.log(`Received message: ${message}`);
|
||||
wss.clients.forEach((client) => {
|
||||
client.send(`Server received your message: ${message}`);
|
||||
});
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('Client disconnected');
|
||||
io.on('connection', (socket) => {
|
||||
console.log('a user connected');
|
||||
socket.on('disconnect', () => {
|
||||
console.log('user disconnected');
|
||||
});
|
||||
});
|
||||
|
||||
const PORT = process.env.SERVER_PORT || 3000;
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`listening on *:${PORT}`);
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user