-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
81 additions
and
42 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
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,34 +1,40 @@ | ||
"use strict"; | ||
const fs = require('fs') | ||
const http = require('http') | ||
const readline = require('readline') | ||
const timer = require('timers') | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}) | ||
|
||
|
||
const hostname = '127.0.0.1' | ||
const port = 3000 | ||
|
||
// Sets the server and loads the webpage. | ||
const server = http.createServer((req, res) => { | ||
res.statusCode = 200 | ||
res.end(fs.readFileSync('index.html')) | ||
}) | ||
|
||
const io = require('socket.io').listen(server) | ||
|
||
// Handles the websocket client connect. | ||
io.sockets.on("connection", socket => { | ||
console.log("User connected: " + socket.id) | ||
socket.on("ClientMessage", x => console.log(x)) | ||
}) | ||
|
||
rl.on('line', input => io.emit('ServerMessage',input)) | ||
|
||
server.listen(port, hostname, () => { | ||
console.log(`Server running at http://${hostname}:${port}/`) | ||
}) | ||
const fs = require('fs') | ||
const http = require('http') | ||
const readline = require('readline') | ||
const timer = require('timers') | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}) | ||
|
||
|
||
const hostname = '127.0.0.1' | ||
const port = 3000 | ||
|
||
// Sets the server and loads the webpage. | ||
const server = http.createServer((req, res) => { | ||
res.statusCode = 201 | ||
res.end(fs.readFileSync('index.html')) | ||
}) | ||
|
||
const io = require('socket.io').listen(server) | ||
|
||
// Handles the websocket client connect. | ||
io.sockets.on("connection", socket => { | ||
console.log("User connected: " + socket.id) | ||
socket.on("ClientMessage", (key, agent) => handleInput(key, agent)) | ||
}) | ||
|
||
const handleInput = (key, agent) => { | ||
console.log(key) | ||
io.emit('UpdatePlayer',{key, agent}) | ||
} | ||
|
||
// Sends a message to all clients. | ||
rl.on('line', input =>io.emit('ServerMessage', input)) | ||
|
||
server.listen(port, hostname, () => { | ||
console.log(`Server running at http://${hostname}:${port}/`) | ||
}) |