Skip to content

Commit

Permalink
web socket demo
Browse files Browse the repository at this point in the history
  • Loading branch information
hnasr committed Aug 18, 2019
1 parent 8d5f6a2 commit b334e17
Show file tree
Hide file tree
Showing 3,483 changed files with 265,534 additions and 4 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
7 changes: 3 additions & 4 deletions fetchlazy/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<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>
Expand Down Expand Up @@ -35,12 +34,12 @@ <h1>createObjectURL vs revokeObjectURL</h1>
})
btnLoad.addEventListener("click", async e => {

const result = await fetch("http://localhost:8081/test.png")
const result = await fetch("http://localhost:8080/test.png")
const blob = await result.blob()
objUrl = URL.createObjectURL(blob)
alert(`Image loaded! The size of your image is ${blob.size} Object url ${objUrl}`)
})

</script>
</body>
</html>
</html>
12 changes: 12 additions & 0 deletions web-server-from-scratch/index.html
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>
13 changes: 13 additions & 0 deletions web-server-from-scratch/index.js
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"))
48 changes: 48 additions & 0 deletions websocket-demo/index.js
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")
1 change: 1 addition & 0 deletions websocket-demo/node_modules/.bin/atob

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

1 change: 1 addition & 0 deletions websocket-demo/node_modules/.bin/color-support

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

1 change: 1 addition & 0 deletions websocket-demo/node_modules/.bin/gulp

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

1 change: 1 addition & 0 deletions websocket-demo/node_modules/.bin/semver

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

1 change: 1 addition & 0 deletions websocket-demo/node_modules/.bin/which

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

21 changes: 21 additions & 0 deletions websocket-demo/node_modules/ansi-colors/LICENSE

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

105 changes: 105 additions & 0 deletions websocket-demo/node_modules/ansi-colors/README.md

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

Loading

0 comments on commit b334e17

Please sign in to comment.