-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsocket.server.with.fork.php
More file actions
58 lines (55 loc) · 2.01 KB
/
websocket.server.with.fork.php
File metadata and controls
58 lines (55 loc) · 2.01 KB
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
<?php
// setup
$port = 5000; // port to listen to
$wdir = '/web/websockets'; // will write delay stats to wdir/sleeps.hcsv
chdir( $wdir);
// run
require_once( 'requireme2.php');
function tsystem() { // epoch of system time
$list = @gettimeofday();
return ( double)( $list[ 'sec'] + 0.000001 * $list[ 'usec']);
}
class MyWebSocketServer extends WebSocketServerStreamingWithFork {
var $delay;
//protected $maxBufferSize = 1048576; //1MB... overkill for an echo server, but potentially plausible for other applications.
protected function rx( $user, $message) {
global $wdir;
$L = explode( ' ', $message);
$pos = array_shift( $L); $blocksize = array_shift( $L);
$step = array_shift( $L); $thru = array_shift( $L); $until = array_shift( $L);
echo "received($message) pos=$pos,blocksize=$blocksize,step=$step,thru=$thru,until=$until\n";
// calculate per-block request delay
$user->in = fopen( 'data.binary', 'r');
//if ( ! $user->in || feof( $user->in)) return $user->disconnect( $user->socket);
$user->pos = $pos;
$user->blocksize = $blocksize;
$user->step = $step;
$user->lastpos = $until;
}
protected function tx( $user) {
if ( ! $user->in || @feof( $user->in)) return false; // no file to read from
if ( $user->pos >= $user->lastpos) { @fclose( $user->in); return false; }
rewind( $user->in);
echo ' ' . $user->pos;
fseek( $user->in, $user->pos);
$msg = fread( $user->in, $user->blocksize);
$this->send( $user, $msg, 'binary');
$user->pos += $user->step * $user->blocksize;
//usleep( $this->delay);
return true;
}
protected function connected( $user) {
echo "new client\n";
$out = fopen( 'websocket.client.log', 'a');
fwrite( $out, "user=" . $user->id . ",action=connected,time=" . tsystem() . "\n");
fclose( $out);
}
protected function closed( $user) {
echo "client closed\n";
$out = fopen( 'websocket.client.log', 'a');
fwrite( $out, "user=" . $user->id . ",action=closed,time=" . tsystem() . "\n");
fclose( $out);
}
}
$echo = new MyWebSocketServer( '0.0.0.0', $port, 2048, 10);
?>