Parcourir la Source

serve everything using express.

Lucas Stadler 12 ans auparavant
Parent
commit
87cf1c4caa
3 fichiers modifiés avec 13 ajouts et 4 suppressions
  1. 2 1
      js/pixl/package.json
  2. 0 0
      js/pixl/public/index.html
  3. 11 3
      js/pixl/server.js

+ 2 - 1
js/pixl/package.json

3
	"description": "server for a collaborative, programmable, pixel-placing game.",
3
	"description": "server for a collaborative, programmable, pixel-placing game.",
4
	"version": "0.0.0-alpha",
4
	"version": "0.0.0-alpha",
5
	"dependencies": {
5
	"dependencies": {
6
		"express": "3.x",
6
		"ws": "0.4.31"
7
		"ws": "0.4.31"
7
	}
8
	}
8
}
9
}

js/pixl/index.html → js/pixl/public/index.html


+ 11 - 3
js/pixl/server.js

1
var ws = require('ws');
1
var ws = require('ws');
2
var http = require('http');
3
var express = require('express');
2
4
3
var server = new ws.Server({port: 8001});
5
var app = express();
6
app.use(express.static(__dirname + "/public"));
7
8
var server = http.createServer(app);
9
server.listen(8001);
10
11
var wss = new ws.Server({server: server});
4
12
5
var world = {};
13
var world = {};
6
14
7
server.on('connection', function(socket) {
15
wss.on('connection', function(socket) {
8
	socket.send(JSON.stringify(world));
16
	socket.send(JSON.stringify(world));
9
17
10
	socket.on('message', function(msg) {
18
	socket.on('message', function(msg) {
14
			console.log(pixl);
22
			console.log(pixl);
15
		});
23
		});
16
24
17
		server.clients.forEach(function(client) {
25
		wss.clients.forEach(function(client) {
18
			if (socket != client) {
26
			if (socket != client) {
19
				client.send(msg);
27
				client.send(msg);
20
			}
28
			}