Skip to content

Commit

Permalink
Better host and port handling
Browse files Browse the repository at this point in the history
- server now generates a temporary file named 'wshost.tmp' by default
in the current working directory that contains defined web socket url.

- chat sample takes host[:port] as the first parameter or uses
gethostname to create the instance.
  • Loading branch information
albeva committed Dec 21, 2011
1 parent 2cdd4aa commit dc6ce54
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 3 deletions.
30 changes: 30 additions & 0 deletions WebSocket/Server.php
Expand Up @@ -102,6 +102,16 @@ class Server
protected $serializers = array();


/**
* Create temporary file containing host and port
* information that server was started with at the given path
* false to not create
*
* @var bool|string
*/
protected $temporary = 'wshost.tmp';


/**
* Create new WebSocket server instance.
*
Expand Down Expand Up @@ -169,6 +179,21 @@ function loadConfig(array $config)
}
$this->clientClass = $clientClass;

// temporary file name
if (array_key_exists('tempoaray', $config)) {
if (!$config['tempoaray']) $this->temporary = false;
else $this->temporary = (string)$config['tempoaray'];
}

// resolve temporary path
if ($this->temporary) {
if ($this->temporary[0] == '/' || preg_match('/^[a-zA-Z]:\\\/', $this->temporary)) {
$this->temporary = $this->temporary;
} else {
$this->temporary = getcwd() . '/' . $this->temporary;
}
}

// serializer
if (isset($config['serializer'])) {
$type = $config['serializer'];
Expand Down Expand Up @@ -215,6 +240,11 @@ function start()
$this->sockets[(int)$master] = $master;
$this->established = microtime(true);

// write temporary
if ($this->temporary) {
file_put_contents($this->temporary, "ws://{$this->host}:{$this->port}");
}

// log
$this->log('Server started: ' . date('Y-m-d H:i:s'));
$this->log('Master socket : ' . $master);
Expand Down
77 changes: 77 additions & 0 deletions examples/chat/index.php
@@ -0,0 +1,77 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>PHP WebSocket Chat sample</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" media="all">
<link rel="stylesheet" type="text/css" href="css/chat.css" media="all">
<script type="text/javascript">
window.WEB_SOCKET_SWF_LOCATION = "web-socket-js/WebSocketMain.swf";
window.socket_host = "<?= file_get_contents('wshost.tmp') ?>";
</script>
<script type="text/javascript" src="web-socket-js/swfobject.js"></script>
<script type="text/javascript" src="web-socket-js/web_socket.js"></script>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap-modal.js"></script>
<script type="text/javascript" src="js/modernizr.custom.81235.js"></script>
<script type="text/javascript" src="js/client.js"></script>
<script type="text/javascript" src="js/ui.js"></script>
<script type="text/javascript" src="js/app.js"></script>
</head>
<body>
<div id="wrapper">
<div class="page-header">
<h1>PHP WebSocket Chat sample</h1>
</div>
<div id="main">
<div id="content">
<div id="chat-area">
</div>
<div id="input-area">
<input type="text" name="message" id="message">
<button id="say" class="btn primary">Say</button>
</div>
</div>
<div id="sidebar">
<div id="list"></div>
</div>
</div>
</div>
<!-- sample modal content -->
<div id="details" class="modal hide fade">
<div class="modal-header">
<a href="#" class="close">&times;</a>
<h3>Your details</h3>
</div>
<div class="modal-body">
<form action="?">
<div class="clearfix">
<label for="nickname">Your Nickname</label>
<div class="input">
<input id="nickname" class="xlarge" type="text" maxlength="10" name="nickname">
</div>
</div>
<div class="clearfix">
<label>Your Avatar</label>
<div class="input">
<ul class="avatar-select">
<li class="avatar-1 active"></li>
<li class="avatar-2"></li>
<li class="avatar-3"></li>
<li class="avatar-4"></li>
<li class="avatar-5"></li>
<li class="avatar-6"></li>
<li class="avatar-7"></li>
<li class="avatar-8"></li>
</ul>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<a href="#" class="btn save primary">Save</a>
<a href="#" class="btn disconnect danger">Disconnect</a>
</div>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion examples/chat/js/app.js
Expand Up @@ -19,7 +19,7 @@ $(function() {
clients = {},
connected = false,
socket = null,
host = 'ws://173.203.111.216:12345',
host = window.socket_host,
WSocket = window.MozWebSocket || window.WebSocket,
myself = null,
setSettings, send, connect, close, onOpen, onMessage, onClose, say;
Expand Down
12 changes: 10 additions & 2 deletions examples/chat/server.php
Expand Up @@ -76,9 +76,17 @@ function receive($msg)

// Create server
try {
// detect host and port
$host = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : gethostname();
$port = 12345;
if (strpos($host, ':') !== false) {
list($host, $port) = explode(':', $host);
}

// create the server instance
$server = new WebSocket\Server(array(
'host' => '173.203.111.216',
'port' => 12345,
'host' => $host,
'port' => $port,
'clientClass' => 'Chatter',
'serializer' => 'json'
));
Expand Down
1 change: 1 addition & 0 deletions examples/chat/wshost.tmp
@@ -0,0 +1 @@
ws://localhost:9876

0 comments on commit dc6ce54

Please sign in to comment.