-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1> HEY I WROTE ALL OF THIS MYSELF! NODEJS BABES</h1> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const app = require("express")() | ||
|
||
app.get("/", (req, res) => { | ||
|
||
res.sendFile(`${__dirname}/index.html`) | ||
}) | ||
|
||
app.get("/sup", (req,res) => { | ||
res.statusCode = 418; | ||
res.send("Hello!"); | ||
}) | ||
|
||
app.listen(8080, ()=>console.log("SUP I'm listening yes sir. what do you want")) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const http = require("http"); | ||
const WebSocketServer = require("websocket").server | ||
let connection = null; | ||
|
||
//create a raw http server (this will help us create the TCP which will then pass to the websocket to do the job) | ||
const httpserver = http.createServer((req, res) => { | ||
|
||
console.log("we have received a request"); | ||
}) | ||
|
||
//pass the httpserver object to the WebSocketServer library to do all the job, this class will override the req/res | ||
const websocket = new WebSocketServer({ | ||
"httpServer": httpserver | ||
}) | ||
//when a legit websocket request comes listen to it and get the connection .. once you get a connection thats it! | ||
websocket.on("request", request=> { | ||
|
||
connection = request.accept(null, request.origin) | ||
connection.on("open", () => console.log("Opened!!!")) | ||
connection.on("close", () => console.log("CLOSED!!!")) | ||
connection.on("message", message => { | ||
|
||
console.log(`Received message ${message.utf8Data}`) | ||
}) | ||
|
||
|
||
//use connection.send to send stuff to the client | ||
sendevery5seconds(); | ||
|
||
|
||
}) | ||
|
||
httpserver.listen(8080, () => console.log("My server is listening on port 8080")) | ||
|
||
function sendevery5seconds(){ | ||
|
||
connection.send(`Message ${Math.random()}`); | ||
|
||
setTimeout(sendevery5seconds, 5000); | ||
|
||
|
||
} | ||
|
||
|
||
//client code | ||
//let ws = new WebSocket("ws://localhost:8080"); | ||
//ws.onmessage = message => console.log(`Received: ${message.data}`); | ||
//ws.send("Hello! I'm client") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.