websockets in faces3.php

This commit is contained in:
Ruben 2016-11-01 22:35:27 +01:00
parent 510f7bd442
commit 44f8d68f50
2 changed files with 28 additions and 1 deletions

1
bin/socket_server.php Normal file → Executable file
View file

@ -1,3 +1,4 @@
#!/usr/bin/php
<?php <?php
chdir(__DIR__); chdir(__DIR__);

View file

@ -44,7 +44,6 @@ EOSNIPPET;
<head> <head>
<title>Emotion Hero</title> <title>Emotion Hero</title>
</head> </head>
<meta http-equiv="refresh" content="30" />
<style type="text/css"> <style type="text/css">
html{ html{
@ -414,5 +413,32 @@ EOSVG;
echo "</div>"; // .currentHit echo "</div>"; // .currentHit
?> ?>
<script type="text/javascript">
var socket = new WebSocket("ws://<?php echo $_SERVER['SERVER_NAME']; ?>:8181");
connection.onopen = function(){
/*Send a small message to the console once the connection is established */
console.log('Connection open!');
};
connection.onclose = function(){
console.log('Connection closed');
connection.connect()
}
connection.onerror = function(error){
// reload page
console.log('Error detected: ' + error);
location.reload();
}
connection.onmessage = function(e){
var message = JSON.parse(e.data);
// var server_message = e.data;
console.log(message);
if(message['action'] == 'update') {
if(message['method'] == 'image' || message['method'] == 'game') {
// reload page
location.reload();
}
}
}
</script>
</body> </body>
</html> </html>