-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexample.js
96 lines (77 loc) · 3.04 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*============================================================================*\
Example of webserver using PHP and websockets served on the same port (80)
(c) Paragi Aps, Simon Riget 2015.
Free to use, provided copyright note is preserved
This example is showing how to make a:
* Snappy PHP serverside scripting
* Websocket support, utilising PHP script to serve requests
* Transferring node sessions to PHP
Sessions are created on a regular HTTP page request. The generated session are
then used with the websocket request, identified by the same session ID cookie
Note: This example depends on the modules: express, ws express-session and
body-parser:
npm install express ws express-session body-parser
The script php_worker.php is always called, to set globals correctly etc.
The requested script are included by this script.
\*============================================================================*/
// Catch missing modules
process.on('uncaughtException', function(err) {
console.error("example.js requires modules installed. Use:"
+ "\n\n npm install express express-session ws body-parser"
+ "\n\nError: ",err.message);
});
// Load modules
var express = require('express');
var expressSession = require('express-session');
var bodyParser = require('body-parser');
var _ws = require('ws');
var path = require('path');
process.removeAllListeners('uncaughtException');
// Initialize server
var sessionStore = new expressSession.MemoryStore();
var sphp = require('./sphp.js');
var app = express();
var server = app.listen(8080,'0.0.0.0','',function () {
console.log('Server listening at://%s:%s'
,server.address().address
,server.address().port);
});
var ws = new _ws.Server({server: server});
// Set up session. store and name must be set, for sphp to catch it
var docRoot = module.filename.substring(0,module.filename.lastIndexOf(path.sep)) + '/example/';
var sessionOptions={
store: sessionStore
,secret:'yes :c)'
,resave:false
,saveUninitialized:false
,rolling: true
,name: 'SID'
}
var sphpOptions = {
overwriteWSPath: "/ws_request.php"
,docRoot: docRoot
}
/*============================================================================*\
Middleware
\*============================================================================*/
// Attach session control
app.use(expressSession(sessionOptions));
// Save some session specific data
app.use(function(request, response, next){
request.session.ip=request.client.remoteAddress;
next();
});
// Parsing POST requests (Not for websockets)
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({extended: true})); // to support URL-encoded bodies
// Attach sPHP
app.use(sphp.express(sphpOptions));
// Attach sPHP execution to the websocket connect event
ws.on('connection',sphp.websocket(sessionOptions));
// Setup html file server
app.use(function(request, response, next){
if(request._parsedUrl.pathname == '/')
request._parsedUrl.pathname='/example.html';
next();
});
app.use(express.static(docRoot));