Spaceship operator <=> (組合比較符)
spaceship operator 是用來比較兩個值$a是大於, 等於或小於$b. 當
- $a 大於 $b -> 回傳 1
- $a 等於 $b -> 回傳 0
- $a 小於 $b -> 回傳 -1
<?php
//產生加完時要用到的KEY
$rand_key = random_bits(16);
$string = '1234567890,A,b,c';
echo 'string to encrypted '.$string;
echo "<br>";
$encrypted_str = aes_encrypt($string);
echo 'encrypted string : '.$encrypted_str;
echo "<br>";
echo 'decrypted string : '.aes_decrypt($encrypted_str);
function aes_encrypt($str){
global $rand_key;
$key = $rand_key;
$encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB);
//也可以用base64_encode來編碼, 只要注意你用什麼編就要用什麼反編
return bin2hex($encrypted);
}
function aes_decrypt($str){
global $rand_key;
$key = $rand_key;
//先前用16進制編碼的就用16進制反編回去
$str = hex2bin($str);
$str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB);
return $str;
}
function random_bits($l = 8) {
return substr(md5(uniqid(mt_rand(), true)), 0, $l);
}
?>
屬性/方法 | 說明 |
---|---|
url | EventSource 中指定的 URL |
readystate | EventSource 的狀態 |
CONNECTING | 0 : 代表試著與SERVER連線 |
OPEN | 1 : 連線成功 |
CLOSE | 2 : 關閉連線 |
onopen | 連線成功時會呼叫的事件 |
onmessage | 接收到資料的時候會呼叫的事件 |
error | 發生錯誤時會呼叫的事件, 發生錯誤後不會重試連線 |
close() | 中斷與SERVER的連線, 中斷後不會再重試連線 |
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="body">
<div class="container">
<header>
<button onclick="stop();">stop</button>
</header>
<main>
<div id="result"></div>
<div id="ce"></div>
</main>
</div>
</div>
<script>
var source = new EventSource("serverside.php");
source.onmessage = function(event) {
console.log('new message, msg type', typeof(event.data));
document.getElementById("result").innerHTML = event.data + "<br>";
};
function stop(){
source.close();
}
</script>
</body>
</html>
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$time = date('r');
echo "data: The server time is: {$time}\n\n";
?>
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
echo "event: myEvent_1\n";
echo "data: my Custom event msg {$time}\n";
echo 'data:' . json_encode(array("username"=> "John123", "status"=> "online")) . "\n\n";
echo "event: myEvent_2\n";
echo "data: my Custom event msg {$time}\n";
echo 'data:' . json_encode(array("title"=> "notification", "msg"=> "You've got mail")) . "\n\n";
?>
<script>
source.addEventListener('myEvent_1', function(event){
var obj = JSON.parse(event.data.split('\n')[1]);
document.getElementById("ev1").innerHTML += obj.username+" : "+obj.status+"<br>";
}, false)
source.addEventListener('myEvent_2', function(event){
var obj = JSON.parse(event.data.split('\n')[1]);
document.getElementById("ev2").innerHTML += obj.title +" : "+obj.msg+"<br>";
}, false)
</script>