new main project start
This commit is contained in:
75
crashsourceFiles/index.html
Normal file
75
crashsourceFiles/index.html
Normal file
@@ -0,0 +1,75 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Websocket tutorial</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; padding: 20px; background: #121212 ; color: #ffff;}
|
||||
#log {background: #0000; padding: 10px; height: 300px; overflow-y: scroll; border: 1px solid #333;}
|
||||
.status-on {color: #00ff00}
|
||||
.status-off {color: #ff0000;}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>WebSocket Broadcast Console</h1>
|
||||
<p id="status" class="status-off">Connecting...</p>
|
||||
|
||||
<form id="message-form">
|
||||
<input
|
||||
id="message-input"
|
||||
type="text"
|
||||
placeholder="Send cargo..."
|
||||
required
|
||||
/>
|
||||
<button
|
||||
type="submit">
|
||||
Deploy Message
|
||||
</button>
|
||||
</form>
|
||||
<h3>Live Steam Logs:</h3>
|
||||
<pre id="log"></pre>
|
||||
</body>
|
||||
<script>
|
||||
const statusEl = document.getElementById('status')
|
||||
const log = document.getElementById('log')
|
||||
const input = document.getElementById('message-input')
|
||||
|
||||
// 1. creates the connection
|
||||
const s = new WebSocket('ws://localhost:8081')
|
||||
|
||||
const appendLog = (label, message) =>{
|
||||
const entry = `${new Date().toLocaleTimeString()} ${label}: ${message}\n`
|
||||
log.textContent = entry + log.textContent
|
||||
}
|
||||
|
||||
s.addEventListener('open', ()=>{
|
||||
statusEl.textContent = 'CONNECTED: ws://localhost:8081'
|
||||
statusEl.className = 'status-on'
|
||||
appendLog('[SYSTEM]', 'Tunnel Established')
|
||||
})
|
||||
|
||||
s.addEventListener('close', ()=>{
|
||||
statusEl.textContent = 'Disconnected from: ws://localhost:8081'
|
||||
statusEl.className = 'status-off'
|
||||
appendLog('[SYSTEM]', 'Tunnel Disconnected')
|
||||
})
|
||||
|
||||
s.addEventListener('message', (e)=>{
|
||||
appendLog('[RECEIVED]', e.data)
|
||||
})
|
||||
|
||||
document.getElementById("message-form").addEventListener('submit', (e)=>{
|
||||
e.preventDefault()
|
||||
if(s.readyState !== WebSocket.OPEN){
|
||||
appendLog('[ERROR]', 'Not Connected')
|
||||
}
|
||||
|
||||
const msg = input.value.trim()
|
||||
s.send(msg)
|
||||
appendLog('[SENT]', msg)
|
||||
input.value = ''
|
||||
})
|
||||
|
||||
</script>
|
||||
</html>
|
||||
25
crashsourceFiles/package.json
Normal file
25
crashsourceFiles/package.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "websocketcourse",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "server.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "node --watch server.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.tuffraid.net/cowch/webSocketCourse.git"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ws": "^8.19.0",
|
||||
"wscat": "^6.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^25.1.0",
|
||||
"@types/ws": "^8.18.1"
|
||||
}
|
||||
}
|
||||
37
crashsourceFiles/server.js
Normal file
37
crashsourceFiles/server.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import {WebSocketServer, WebSocket} from 'ws'
|
||||
|
||||
const wss = new WebSocketServer({port: 8081})
|
||||
|
||||
// connection event
|
||||
|
||||
/**
|
||||
* Connection types
|
||||
* 0: connecting
|
||||
* 1: open (the only state where you can safely .send())
|
||||
* 2: Closing
|
||||
* 3: closed
|
||||
*/
|
||||
|
||||
wss.on('connection', (s, r)=>{
|
||||
const ip = r.socket.remoteAddress
|
||||
|
||||
s.on('message', (rawData)=>{
|
||||
const message = rawData.toString()
|
||||
console.log({rawData})
|
||||
|
||||
wss.clients.forEach((client)=>{
|
||||
// can use readyState to be === 1 or WebSocket.OPEN they mean the same thing
|
||||
if(client.readyState === WebSocket.OPEN) client.send(`Server Broadcast: ${message}`)
|
||||
})
|
||||
})
|
||||
|
||||
s.on("error", ()=>{
|
||||
console.error(`Error: ${err.message}: ${ip}`)
|
||||
})
|
||||
|
||||
s.on('close', ()=>{
|
||||
console.log('Client Disconnected')
|
||||
})
|
||||
})
|
||||
|
||||
console.log("Server Running on 8081")
|
||||
Reference in New Issue
Block a user