WebSocket是基于Socket的吗?为什么我只收到头信息?

jdgdf566 2013-10-01 03:09:26
在chrome(新版支持WebSocket)里面打开的HTML页面:
<html>
<body>

<script type="text/javascript">
// 创建一个Socket实例
var socket = new WebSocket('ws://localhost:12345');

// 打开Socket
socket.onopen = function(event) {

// 发送一个初始化消息
socket.send('I am the client and I\'m listening!');

// 监听消息
socket.onmessage = function(event) {
console.log('Client received a message',event);
};

// 监听Socket的关闭
socket.onclose = function(event) {
console.log('Client notified socket has closed',event);
};

// 关闭Socket....
//socket.close()
};
</script>

</body>
</html>

socketServer.php:
<?php

/*
* 不使用apache,cli模式
*/

/**
* 接收端
* 单用户,即单连接
* 单线程
*/
class SocketServer {

protected $ip;
protected $port;
protected $socket;
protected $users;
protected $userIndex = 0;
protected $message;

public function __construct($ip = "127.0.0.1", $port = 12345) {
$this->ip = $ip;
$this->port = $port;
//
self::init();
//
$this->createServer();
$this->log('listenning user...');
$this->listenningUser();
}

protected function createServer() {
$errno;
$errstr;
$this->socket = stream_socket_server("tcp://" . $this->ip . ':' . $this->port, $errno, $errstr);
if (!$this->socket) {
self::log("$errstr ($errno)");
exit();
}
$this->log('server ok .');
}

protected function listenningUser() {
while (true) {
$this->userIndex++;
$user = $this->users[$this->userIndex] = stream_socket_accept($this->socket, 9999999999);
//
if (is_resource($this->users[$this->userIndex - 1])) {
$u = $this->users[$this->userIndex - 1];
$u->close();
$u = NULL;
unset($this->users[$this->userIndex - 1]);
}
//
$this->log('连入新用户');
$this->listenningMessage();
}
}

protected function listenningMessage() {
while (is_resource($this->users[$this->userIndex])) {
$this->message = stream_socket_recvfrom($this->users[$this->userIndex], 10270000);
if (!$this->message) {
$this->closeUser();
break;
}
$this->messageOperate();
}
}

function messageOperate() {
$this->log("收到消息:");
$this->log($this->message);
//mb_strstr($haystack, $needle, $before_needle, $encoding)
$this->sendMessage('done');
}

function sendMessage($msg) {
if($msg===''){
return -1;
}
return stream_socket_sendto($this->users[$this->userIndex], $msg);
}

public function closeUser() {
if (!is_resource($this->users[$this->userIndex]))
return FALSE;
@stream_socket_shutdown($this->users[$this->userIndex], STREAM_SHUT_RDWR);
@fclose($this->users[$this->userIndex]);
$this->log("用户连接断开.");
return TRUE;
}

public function shutdown() {
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
fclose($this->socket);
}

protected static function init() {
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);
ob_implicit_flush();
date_default_timezone_set('Asia/Shanghai');
ignore_user_abort(TRUE);
mb_internal_encoding('gbk');
}

protected static function log($message) {
echo "\r\n" . $message . "\r\n";
}

}




$server = new SocketServer();






socketServer.php的输出:
GET / HTTP/1.0
Upgrade: websocket
Connection: Upgrade
Host: localhost:12345
Origin: null
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: 2KtpijUeAXE/WdXJRB8u+w==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/29.0.1547.76 Safari/537.36

也就是说,服务端是socket,客户端是websocket。
没有收到socket.send('I am the client and I\'m listening!'); 这句发送的消息。
...全文
4986 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
shoot136464153 2014-10-17
  • 打赏
  • 举报
回复
你先搞清楚websocket和socket的区别! websocket是应用层协议,底层采用tcp协议,socket是一种实现tcpip协议的工具!也就是说你可以用socket编写支持websocket的应用。你的那句话应该问 websocket比tcp节省服务器资源和带宽吗 那么问题的答案就是 一个应用层协议和一个传输层协议 你自己说区别.
jdgdf566 2013-10-14
  • 打赏
  • 举报
回复
引用 4 楼 iasky 的回复:
[quote=引用 2 楼 jdgdf566 的回复:] [quote=引用 1 楼 iasky 的回复:] 你的server没有做握手操作,客户端请求的这个就是握手,参考phpwebsocket吧 流程: 1. Client try to connect to WebSocket server 2. Server recognize client 3. If client is not registered with server then add client (this is known as handshaking process which is based on headers transmission) 4. Send and receive data 5. Close connection 或者看看这个文章:http://blog.cuelogic.co.in/php-websocket-server-and-client-communication/
为什么浏览器不直接用socket,websocket有什么好处?节省流量吗?[/quote] 原来浏览器只能是http的单向请求,现在websockets可以交互了。htlm5定义的websocket节省服务器资源和带宽并达到实时通讯。[/quote]htlm5定义的websocket比socket节省服务器资源和带宽吗?
jdgdf566 2013-10-12
  • 打赏
  • 举报
回复
我的意思是,websocket与socket比较,不是websocket与http比较。另造一种插座有什么好处。
iasky 2013-10-02
  • 打赏
  • 举报
回复
引用 2 楼 jdgdf566 的回复:
[quote=引用 1 楼 iasky 的回复:] 你的server没有做握手操作,客户端请求的这个就是握手,参考phpwebsocket吧 流程: 1. Client try to connect to WebSocket server 2. Server recognize client 3. If client is not registered with server then add client (this is known as handshaking process which is based on headers transmission) 4. Send and receive data 5. Close connection 或者看看这个文章:http://blog.cuelogic.co.in/php-websocket-server-and-client-communication/
为什么浏览器不直接用socket,websocket有什么好处?节省流量吗?[/quote] 原来浏览器只能是http的单向请求,现在websockets可以交互了。htlm5定义的websocket节省服务器资源和带宽并达到实时通讯。
jdgdf566 2013-10-02
  • 打赏
  • 举报
回复
为什么浏览器不直接用socket,websocket有什么好处?节省流量吗? websocket还要对 origin timeout ssl connection_id 等等在做处理。 简直是有病。
jdgdf566 2013-10-02
  • 打赏
  • 举报
回复
引用 1 楼 iasky 的回复:
你的server没有做握手操作,客户端请求的这个就是握手,参考phpwebsocket吧 流程: 1. Client try to connect to WebSocket server 2. Server recognize client 3. If client is not registered with server then add client (this is known as handshaking process which is based on headers transmission) 4. Send and receive data 5. Close connection 或者看看这个文章:http://blog.cuelogic.co.in/php-websocket-server-and-client-communication/
为什么浏览器不直接用socket,websocket有什么好处?节省流量吗?
jdgdf566 2013-10-02
  • 打赏
  • 举报
回复
引用 4 楼 iasky 的回复:
[quote=引用 2 楼 jdgdf566 的回复:] [quote=引用 1 楼 iasky 的回复:] 你的server没有做握手操作,客户端请求的这个就是握手,参考phpwebsocket吧 流程: 1. Client try to connect to WebSocket server 2. Server recognize client 3. If client is not registered with server then add client (this is known as handshaking process which is based on headers transmission) 4. Send and receive data 5. Close connection 或者看看这个文章:http://blog.cuelogic.co.in/php-websocket-server-and-client-communication/
为什么浏览器不直接用socket,websocket有什么好处?节省流量吗?[/quote] 原来浏览器只能是http的单向请求,现在websockets可以交互了。htlm5定义的websocket节省服务器资源和带宽并达到实时通讯。[/quote]我的意思是,websocket与socket比较,不是websocket与http比较。另造一种插座有什么好处。
iasky 2013-10-01
  • 打赏
  • 举报
回复
你的server没有做握手操作,客户端请求的这个就是握手,参考phpwebsocket吧

流程:


1. Client try to connect to WebSocket server
2. Server recognize client
3. If client is not registered with server then add client (this is known as handshaking process which is based on headers transmission)
4. Send and receive data
5. Close connection


或者看看这个文章:http://blog.cuelogic.co.in/php-websocket-server-and-client-communication/

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧