Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
basten committed May 7, 2012
0 parents commit 11e1d20
Show file tree
Hide file tree
Showing 10 changed files with 720 additions and 0 deletions.
Binary file added client/C64_Pro_Mono_v1.0-STYLE.eot
Binary file not shown.
Binary file added client/C64_Pro_Mono_v1.0-STYLE.ttf
Binary file not shown.
Binary file added client/C64_Pro_Mono_v1.0-STYLE.woff
Binary file not shown.
25 changes: 25 additions & 0 deletions client/c64.css
@@ -0,0 +1,25 @@
@CHARSET "ISO-8859-1";
body {
background-color:#a5a5ff;
font-family:"c64";
color:#a5a5ff;
}
#terminal {
padding:12px;
position:absolute;
top: 50%;
left: 50%;
width:50em;
height:30em;
margin-top: -15em; /*set to a negative number 1/2 of your height*/
margin-left: -25em; /*set to a negative number 1/2 of your width*/
background-color:#4242e7;
}
@font-face {
font-family: 'c64';
src: url('C64_Pro_Mono_v1.0-STYLE.eot');
src: url('C64_Pro_Mono_v1.0-STYLE.eot?#iefix') format('embedded-opentype'),
url('C64_Pro_Mono_v1.0-STYLE.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
97 changes: 97 additions & 0 deletions client/index.html
@@ -0,0 +1,97 @@
<!DOCTYPE html>
<html style="height:100%;">
<head>
<meta charset="ISO-8859-1">
<link type="text/css" rel="stylesheet" href="c64.css"/>
<title>C64</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
var context = $("#terminal");

var str = 'HELLO, THIS IS A SIMPLE TEST.';
var line = new Array();

for(i = 0; i < str.length; i++) {
line.push(str[i]);
}
line.reverse();

var write = function() {
$("#terminal").html($("#terminal").html() + line.pop());
if (line.length > 0)
{
//console.log(line);

setTimeout ('write()',100);
}
}

$(document).ready( function(){

//write();

$(document).bind('keypress',function(event) {
console.log(event.charCode);
//if(event.charCode == 0)
//{
// $("#terminal").html($("#terminal").html() + "<br />");
//}
//else {
// $("#terminal").html($("#terminal").html() + String.fromCharCode(event.charCode));
//}
if(event.charCode == 0 || event.charCode == 13) {
send("<br />");
}
else {
send(String.fromCharCode(event.charCode));
}
});

init()
});

</script>

<script>
var socket;

function init(){
var host = "ws://CONTENT6818:12346/phpwebsocket/server/server.php";
try{

if ("WebSocket" in window)
{
socket = new WebSocket(host);
}
else if ("MozWebSocket" in window)
{
socket = new MozWebSocket(host);
}
//socket = new MozWebSocket(host);
$("#terminal").html($("#terminal").html() + 'STATUS \"'+socket.readyState + "\"<br />");



socket.onopen = function(msg){ $("#terminal").html($("#terminal").html() + "STATUS \"" + this.readyState + "\"<br />"); };
socket.onmessage = function(msg){ $("#terminal").html($("#terminal").html() + msg.data); };
socket.onclose = function(msg){ $("#terminal").html($("#terminal").html() + "STATUS \""+this.readyState + "\"<br />"); };
}
catch(ex){ $("#terminal").html($("#terminal").html() + "<br /> " + ex)}
}

function send(msg){
try{ socket.send(msg); } catch(ex){ log(ex); }
}
function quit(){
$("#terminal").html($("#terminal").html() + "<br />Goodbye!");
socket.close();
socket=null;
}
</script>

</head>
<body style="">
<div id="terminal">
</div>
</body>
</html>
24 changes: 24 additions & 0 deletions server/chatbot.demo.php
@@ -0,0 +1,24 @@
#!/php -q
<?php
// Run from command prompt > php -q chatbot.demo.php
include "websocket.class.php";

// Extended basic WebSocket as ChatBot
class ChatBot extends WebSocket{
function process($user,$msg){
$this->say("< ".$msg);
switch($msg){
case "hello" : $this->send($user->socket,"hello human"); break;
case "hi" : $this->send($user->socket,"zup human"); break;
case "name" : $this->send($user->socket,"my name is Multivac, silly I know"); break;
case "age" : $this->send($user->socket,"I am older than time itself"); break;
case "date" : $this->send($user->socket,"today is ".date("Y.m.d")); break;
case "time" : $this->send($user->socket,"server time is ".date("H:i:s")); break;
case "thanks": $this->send($user->socket,"you're welcome"); break;
case "bye" : $this->send($user->socket,"bye"); break;
default : $this->send($user->socket,$msg." not understood"); break;
}
}
}

$master = new ChatBot("localhost",12345);
67 changes: 67 additions & 0 deletions server/client.html
@@ -0,0 +1,67 @@
<html>
<head>
<title>WebSocket</title>

<style>
html,body{font:normal 0.9em arial,helvetica;}
#log {width:440px; height:200px; border:1px solid #7F9DB9; overflow:auto;}
#msg {width:330px;}
</style>

<script>
var socket;

function init(){
var host = "ws://CONTENT6818:12346/websocket/server.php";
try{

if ("WebSocket" in window)
{
socket = new WebSocket(host);
}
else if ("MozWebSocket" in window)
{
socket = new MozWebSocket(host);
}
//socket = new MozWebSocket(host);
console.log(socket);
log('WebSocket - status '+socket.readyState);
socket.onopen = function(msg){ log("Welcome - status "+this.readyState); };
socket.onmessage = function(msg){ log("Received: "+msg.data); };
socket.onclose = function(msg){ log("Disconnected - status "+this.readyState); };
}
catch(ex){ log(ex); }
$("msg").focus();
}

function send(){
var txt,msg;
txt = $("msg");
msg = txt.value;
if(!msg){ alert("Message can not be empty"); return; }
txt.value="";
txt.focus();
try{ socket.send(msg); log('Sent: '+msg); } catch(ex){ log(ex); }
}
function quit(){
log("Goodbye!");
socket.close();
socket=null;
}

// Utilities
function $(id){ return document.getElementById(id); }
function log(msg){ $("log").innerHTML+="<br>"+msg; }
function onkey(event){ if(event.keyCode==13){ send(); } }
</script>

</head>
<body onload="init()">
<h3>WebSocket v2.00</h3>
<div id="log"></div>
<input id="msg" type="textbox" onkeypress="onkey(event)"/>
<button onclick="send()">Send</button>
<button onclick="quit()">Quit</button>
<div>Commands: hello, hi, name, age, date, time, thanks, bye</div>
</body>
</html>
166 changes: 166 additions & 0 deletions server/server.php
@@ -0,0 +1,166 @@
#!/php -q
<?php /* >php -q server.php */

error_reporting(E_ALL & !E_WARNING);
set_time_limit(0);
ob_implicit_flush();

$master = WebSocket("localhost",12346);
$sockets = array($master);
$users = array();
$debug = true;

while(true){
$changed = $sockets;
socket_select($changed,$write=NULL,$except=NULL,NULL);
foreach($changed as $socket){
if($socket==$master){
$client=socket_accept($master);
if($client<0){ console("socket_accept() failed"); continue; }
else{ connect($client); }
}
else{
$bytes = @socket_recv($socket,$buffer,2048,0);
if($bytes==0){ disconnect($socket); }
else{
$user = getuserbysocket($socket);
if(!$user->handshake){ dohandshake($user,$buffer); }
else{ process($user,$buffer); }
}
}
}
}

//---------------------------------------------------------------
function process($user,$msg){
$action = unwrap($msg);
say("< ".$action);
switch($action){
case "hello" : send($user->socket,"hello human"); break;
case "hi" : send($user->socket,"zup human"); break;
case "name" : send($user->socket,"my name is Multivac, silly I know"); break;
case "age" : send($user->socket,"I am older than time itself"); break;
case "date" : send($user->socket,"today is ".date("Y.m.d")); break;
case "time" : send($user->socket,"server time is ".date("H:i:s")); break;
case "thanks": send($user->socket,"you're welcome"); break;
case "bye" : send($user->socket,"bye"); break;
default : send($user->socket,$action." not understood"); break;
}
}

function send($client,$msg){
say("> ".$msg);
$msg = wrap($msg);
socket_write($client,$msg,strlen($msg));
}

function WebSocket($address,$port){
$master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() failed");
socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1) or die("socket_option() failed");
socket_bind($master, $address, $port) or die("socket_bind() failed");
socket_listen($master,20) or die("socket_listen() failed");
echo "Server Started : ".date('Y-m-d H:i:s')."\n";
echo "Master socket : ".$master."\n";
echo "Listening on : ".$address." port ".$port."\n\n";
return $master;
}

function connect($socket){
global $sockets,$users;
$user = new User();
$user->id = uniqid();
$user->socket = $socket;
array_push($users,$user);
array_push($sockets,$socket);
console($socket." CONNECTED!");
}

function disconnect($socket){
global $sockets,$users;
$found=null;
$n=count($users);
for($i=0;$i<$n;$i++){
if($users[$i]->socket==$socket){ $found=$i; break; }
}
if(!is_null($found)){ array_splice($users,$found,1); }
$index = array_search($socket,$sockets);
socket_close($socket);
console($socket." DISCONNECTED!");
if($index>=0){ array_splice($sockets,$index,1); }
}

function dohandshake($user,$buffer){
console("\nRequesting handshake...");
console($buffer);
list($resource,$host,$origin,$strkey1,$strkey2,$data) = getheaders($buffer);
console("Handshaking...");

$pattern = '/[^\d]*/';
$replacement = '';
$numkey1 = preg_replace($pattern, $replacement, $strkey1);
$numkey2 = preg_replace($pattern, $replacement, $strkey2);

$pattern = '/[^ ]*/';
$replacement = '';
$spaces1 = strlen(preg_replace($pattern, $replacement, $strkey1));
$spaces2 = strlen(preg_replace($pattern, $replacement, $strkey2));

if ($spaces1 == 0 || $spaces2 == 0 || $numkey1 % $spaces1 != 0 || $numkey2 % $spaces2 != 0) {
socket_close($user->socket);
console('failed');
return false;
}

$ctx = hash_init('md5');
hash_update($ctx, pack("N", $numkey1/$spaces1));
hash_update($ctx, pack("N", $numkey2/$spaces2));
hash_update($ctx, $data);
$hash_data = hash_final($ctx,true);

$upgrade = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n" .
"Upgrade: WebSocket\r\n" .
"Connection: Upgrade\r\n" .
"Sec-WebSocket-Origin: " . $origin . "\r\n" .
"Sec-WebSocket-Location: ws://" . $host . $resource . "\r\n" .
"\r\n" .
$hash_data;

socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));
$user->handshake=true;
console($upgrade);
console("Done handshaking...");
return true;
}

function getheaders($req){
$r=$h=$o=null;
if(preg_match("/GET (.*) HTTP/" ,$req,$match)){ $r=$match[1]; }
if(preg_match("/Host: (.*)\r\n/" ,$req,$match)){ $h=$match[1]; }
if(preg_match("/Origin: (.*)\r\n/",$req,$match)){ $o=$match[1]; }
if(preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$req,$match)){ $key2=$match[1]; }
if(preg_match("/Sec-WebSocket-Key: (.*)\r\n/",$req,$match)){ $key1=$match[1]; }
if(preg_match("/\r\n(.*?)\$/",$req,$match)){ $data=$match[1]; }
return array($r,$h,$o,$key1,$key2,$data);
}

function getuserbysocket($socket){
global $users;
$found=null;
foreach($users as $user){
if($user->socket==$socket){ $found=$user; break; }
}
return $found;
}

function say($msg=""){ echo $msg."\n"; }
function wrap($msg=""){ return chr(0).$msg.chr(255); }
function unwrap($msg=""){ return substr($msg,1,strlen($msg)-2); }
function console($msg=""){ global $debug; if($debug){ echo $msg."\n"; } }

class User{
var $id;
var $socket;
var $handshake;
}

?>

0 comments on commit 11e1d20

Please sign in to comment.