⑴ socketio 不是標準的websocket為什麼java寫的client端連不上
php有可用的websocket庫,不需要php-fpm。
目前比較成熟的有swoole(swoole.com),和workman(workman.net)
swoole是c寫的php擴展, 效率比nodejs還要高,workman是純php實現,兩者都號稱可以實現並發百萬TCP連接。
給你個例子:
這個要通過cmd運行的 具體帶的參數有點忘記了
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
//創建一個socket連接 設置參數 綁定 監聽 並且返回
$master = WebSocket("localhost",12345);
//標示是否已經進行過握手了
$is_shaked = false;
//是否已經關閉
$is_closed = true;
//將socket變為一個可用的socket
while(true){
//如果是關閉狀態並且是沒有握手的話 則創建一個可用的socket(貌似第二個條件可以去除)
if($is_closed && !$is_shaked){
if(($sock = socket_accept($master)) < 0){
echo "socket_accept() failed: reason: " . socket_strerror($sock) . "\n";
}
//將關閉狀態修改為false
$is_closed = false;
}
//開始進行數據處理
process($sock);
}
//處理請求的函數
function process($socket){
//先從獲取到全局變數
global $is_closed, $is_shaked;
//從socket中獲取數據
$buffer = socket_read($socket,2048);
//如果buffer返回值為false並且已經握手的話 則斷開連接
if(!$buffer && $is_shaked){
disconnect($socket);
}else{
//如果沒有握手的話則握手 並且修改握手狀態
if($is_shaked == false){
$return_str = dohandshake($buffer);
$is_shaked = true;
}else{
//如果已經握手的話則送入deal函數中進行相應處理
$data_str = decode($buffer); //解析出來的從前端送來的內容
console($data_str);
$return_str = encode(deal($socket, $data_str));
//$return_str = encode($data_str);
}
//將應該返回的字元串寫入socket返回
socket_write($socket,$return_str,strlen($return_str));
}
}
function deal($socket, $msgObj){
$obj = json_decode($msgObj);
foreach($obj as $key=>$value){
if($key == 'close'){
disconnect($socket);
console('close success');
return 'close success';
}else if($key == 'msg'){
console($value."\n");
return $value;
}
}
}
//獲取頭部信息
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)){ $key=$match[1]; }
if(preg_match("/\r\n(.*?)\$/",$req,$match)){ $data=$match[1]; }
return array($r,$h,$o,$key,$data);
}
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 dohandshake($buffer){
list($resource,$host,$origin,$key,$data) = getheaders($buffer);
echo "resource is $resource\n";
echo "origin is $origin\n";
echo "host is $host\n";
echo "key is $key\n\n";
$response_key = base64_encode(sha1($key.'258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));
$return_str = "HTTP/1.1 101 Switching Protocols\r\n".
"Upgrade: websocket\r\n".
"Connection: Upgrade\r\n".
"Sec-WebSocket-Accept: $response_key\r\n\r\n";
return $return_str;
}
function console($msg){
$msg = transToGBK($msg);
echo "$msg\n";
return $msg;
}
function decode($msg="") {
$mask = array();
$data = "";
$msg = unpack("H*",$msg);
$head = substr($msg[1],0,2);
if (hexdec($head{1}) === 8){
$data = false;
} else if (hexdec($head{1}) === 1){
$mask[] = hexdec(substr($msg[1],4,2));
$mask[] = hexdec(substr($msg[1],6,2));
$mask[] = hexdec(substr($msg[1],8,2));
$mask[] = hexdec(substr($msg[1],10,2));
$s = 12;
$e = strlen($msg[1])-2;
$n = 0;
for ($i= $s; $i<= $e; $i+= 2){
$data .= chr($mask[$n%4]^hexdec(substr($msg[1],$i,2)));
$n++;
}
}
return $data;
}
function encode($msg=""){
$frame = array();
$frame[0] = "81";
$msg .= ' is ok';
$len = strlen($msg);
$frame[1] = $len<16?"0".dechex($len):dechex($len);
$frame[2] = ord_hex($msg);
$data = implode("",$frame);
return pack("H*", $data);
}
function transToGBK($s){//UTF8->GBK
//echo $s;
return iconv("UTF-8", "GBK", $s);
return $s;
}
function ord_hex($data){
$msg = "";
$l = strlen($data);
for ($i=0; $i<$l; $i++){
//ord是返回字元串第一個字元的ascii值
//dechex把十進制轉換為十六進制
$msg .= dechex(ord($data{$i}));
}
return $msg;
}
function disconnect($socket){
global $is_shaked, $is_closed;
$is_shaked = false;
$is_closed = true;
socket_close($socket);
}
?>
⑵ socket.io.js具體在哪
ain()
{
int i,a[10];
for(i=0;i<10;)
a[i++]=2*i+1;
for(i=0;i<=9;i++)
printf("%d ",a[i]);
printf("\n%d %d\n",a[5.2],a[5.8]);
}
本例中用一個循環語句給a數組各元素送入奇數值,然後用第二個循環語句輸出各個奇數。在第一個 for語句中,表達式3省略了。在下標變數中使用了表達式i++,用以修改循環變數。當然第二個for語句也可以這樣作,侍手梁C語言允許用表達式表示下標。 程序中最後一個printf語句輸出了兩次a[5]的值,可以看出當下標不為整數時將自動取整。
7.1.3 一維數組的初始化
給數組賦值的方法除了用賦值語句對數組元素逐個賦值外, 還可採用初始化賦值和動態賦值的方法。
數組初始化賦值是指在數組定義時給數組元素賦予初值。數組初始化是在編譯階段進行的。這樣將減少運行時間,提高效率。
初始化賦值的一般形式為:
類型說明符 數組名[常量表達式]={值,值……值};
其老運中在{ }中的各數據值即為各元素的薯握初值,各值之間用逗號間隔。
⑶ js能否連接socket
aflax,有個已經封裝好的js叫做socketJS
原理就是利用flash,如果你的程序不考慮iphone用戶,版可以考慮這個解決權方法
http://download.csdn.net/detail/enjoy_lhl/5672205
可以下載一下,如果你有編程基礎可以很快上手,下載不需要積分的