php/f01Socket.php
<html>
<head>
<title><?php $cnt=7; echo basename(__file__, '.php'); ?></title>
<script type="text/javascript">
<?php
$hostnm = gethostname();
$prot = $hostnm === 'wk13' ? 'ws' : 'wss';
$host = $hostnm === 'wk13' ? "192.168.1.135" : 'wlkl.ch';
$port = 4321;
echo " const sockUri = '$prot://$host:$port';";
?>
const outLi = [];
var cSock = null;
var cnt = 0;
function out(t) {
outLi.unshift(t);
while (outLi.length > 10)
outLi.pop();
var o = document.getElementById('out');
o.innerHTML = outLi.reduce((a, c) => a + '<li>' + c + '</li>', '');
}
function cOpen() {
if (cSock !== null)
return out('client open, but already open');
try {
cSock = new WebSocket(sockUri);
cSock.onmessage = (event) => out("received: " + event.data);
cSock.onerror = (event) => cOut(event);
out('client open setup');
} catch (e) {out('catch opening ' + e)}
}
function cOut(event) {
var m = "sock error: " + event.constructor.name + ' ' + event.type + ':';
for (const p in event)
m += ', ' + p + '=' + event[p];
out(m + ' * ' + event);
}
function cClose() {
if (cSock === null)
return out('client close, but already closed');
const c = cSock;
cSock = null;
c.close();
out('client closed');
}
function cSend() {
/* out('readyState ' + cSock.readyState); */
if (cSock === null)
return out('client send, but socket notopen');
const t = document.getElementById('cTxt').value;
const m = 'client send ' + t + " #" + ++cnt + " at " + Date().toString();
cSock.send(m);
out(m);
}
</script>
</head>
<body>
<?php echo 'start at ' . date('c') . ', pid ' . getmypid() . ", host $hostnm, php " . phpversion(); ?>
<br>socket uri = <b id="uri">?</b>
<script type="text/javascript">
document.getElementById('uri').innerHTML = sockUri;
</script>
<h1>Socket: Sever mit php</h1>
<form action="f01Socket.php" method="post">
<input type ="submit" name="server" value="start"/>
<input type ="submit" name="server" value="ps"/>
REQUEST_URI <?php echo ++$cnt . ' ' . $_SERVER["REQUEST_METHOD"]. ' ' . $_SERVER["REQUEST_URI"] . " get " . print_r($_GET, true) . " post " . print_r($_POST, true); ?>
<br>
<?php
if (isset($_POST['server'])) {
echo "<br> server " . $f = $_POST['server'];
if ($f === 'start') {
echo "<br>starting server";
$r = exec('./f01SocketServer.php >> f01SocketServer.out 2>&1 & echo shell $0 soSe $? jobPid $!', $o, $c); # ; disown $!; echo disown $! rc $?
# $r = exec('ls', $o, $c);
echo "<br>exec code=$c, r=$r, output=" . print_r($o, true);
} else if ($f === 'ps') {
$ps = [];
exec('ps', $ps, $c);
echo "<br>--- ps code=$c<br>" . implode('<br>', $ps);
$r = exec('echo pwd $(pwd) <br>--- tail f01SocketServer.out; tail -15 f01SocketServer.out;', $o, $c); # ; disown $!; echo disown $! rc $?
echo "<br>code=$c<br>" . implode('<br>', $o);
echo '<br>match ' . preg_match_all('/^\s*(\S*)\s.*\s(f01SocketSer.*)/m', implode("\n", $ps), $mm) . ', mm = ' . print_r($mm, true);
for ($i=0; $i<count($mm[1]); $i++) {
$o=[];
echo "<br> kill {$mm[1][$i]} {$mm[2][$i]}: " . exec("kill {$mm[1][$i]}; echo kill code $?", $o, $c) . "code=$c, output=" . implode('<br>', $o);
}
} else {
echo "<br>errot not implemented $f";
}
}
?>
</form>
<h1>Socket: Client mit javascript</h1>
<input type="button" value="open" onclick="cOpen();" />
<input type="button" value="close" onclick="cClose();" />
<input type="button" value="send" onclick="cSend();" />
<input type="text" id="cTxt" maxlength="800" size="30" />
<h1>Output</h1>
<ul id="out">
</ul>
<h1>Source <?php echo __file__; ?> </h1>
<?php $_POST = []; highlight_file(__file__) ?>
<h1>Source f01SocketServer.php;</h1>
<?php $_POST = []; highlight_file('f01SocketServer.php') ?>
</body>
</html>