WebSocket 服务器:Websocketd(PHP篇)

Websocketd 将标准输入(stdin)和标准输出(stdout)作为数据的输入和输出接口,也就是说可以使用任意语言写后台脚本。

下载安装说明

使用 PHP 编写简单的聊天服务器脚本 chat.php:

#!/usr/bin/php
<?php

$stdin = fopen('php://stdin', 'r');
$filename = __DIR__.'/chat.log';
echo 'Please enter your name:'.PHP_EOL;
$user = trim(fgets($stdin));
$message = '['.date('Y-m-d H:i:s').'] '.$user.' joined the chat'.PHP_EOL;
file_put_contents($filename, $message, FILE_APPEND);

echo '['.date('Y-m-d H:i:s').'] Welcome to the chat '.$user.'!'.PHP_EOL;
$pid = pcntl_fork();
//父进程和子进程都会执行下面代码
if ($pid  == - 1 ) {
	die( 'could not fork' );
} else if ($pid) {
	while ($msg = fgets($stdin)) {
		$message = '['.date('Y-m-d H:i:s').'] '.$user.' '.$msg.PHP_EOL;
		file_put_contents($filename, $message, FILE_APPEND);
	}
	pcntl_wait($status);  //等待子进程中断,防止子进程成为僵尸进程。
} else {
	$lastmtime = null;
	$ftell = null;
    // 子进程得到的 $pid 为 0
	while (1) {
		$fp = fopen($filename,  'r');
		if ($fp) {
			$fstat = fstat($fp);
			$mtime = $fstat['mtime'];
			if (!$lastmtime) {
				fseek($fp, 0, SEEK_END);
				$lastmtime = $mtime;
				$ftell = ftell($fp);
			} else if ($lastmtime < $mtime) {
				$lastmtime = $mtime;
				fseek($fp, $ftell);
				while (!feof($fp) && ($line  =  fgets($fp, 4096)) !==  false ) {
					echo $line;
				}
				$ftell = ftell($fp);
			}
			fclose($fp);
		}
		sleep(1);
	}
}

启动 websocketd:

./websocketd --port=8080 php chat.php

客户端 chat.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>websocketd chat example</title>
</head>
<body>

<div id="messageList"></div>
<input id="message" type="text" />
<button id="sendBtn" type="button">发送</button>
<script>
	var ws = new WebSocket('ws://localhost:8080/');
	ws.onopen = function() {
		document.body.style.backgroundColor = '#cfc';
		document.getElementById('messageList').innerHTML += '已连接<br/>';
	};
	ws.onclose = function() {
		document.body.style.backgroundColor = null;
	};
	ws.onmessage = function(event) {
		document.getElementById('messageList').innerHTML += event.data + '<br/>';
	};
	ws.onerror = function(event) {
		document.getElementById('messageList').innerHTML += '发送异常<br/>';
	};
	document.getElementById('sendBtn').onclick = function(){
		ws.send(document.getElementById('message').value);
		document.getElementById('message').value = '';
	};
</script>

</body>
</html>

然后在火狐打开多个 chat.html,便可以实现互发消息了。

转载于:https://my.oschina.net/myleft/blog/916506

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值