随着围绕WebSockets的所有讨论,很难找到关于如何在Google上使用Apache服务器的良好演练.
我们正在开发一个PHP(symfony2)插件,它将不时运行一种聊天实例.我们发现WebSockets在这方面比AJAX更有趣,更标准,更快捷.问题是,我们的团队中没有太多系统管理员资源,我们发现很难收集有关以下事项的良好信息:
非常感谢你,
ps:我们将链接回您的博客/网站,因为我们将在我们的devblog上发布有关此部分应用的技术/信息性帖子.
再次感谢你!
我得到了一个用Laravel 4编写的web应用程序.这个应用程序使用了Ratchet,更具体地说,它使用了Latchet软件包.作为旁注,我使用以下技术:
现在我得到以下场景:
在我的routes.php中,我有以下代码,以便正确注册主题:
//routes.php
// Setup a connection and register a topic where clients can connect to.
Latchet::connection('Connection');
Latchet::topic('PhotoStream/{client}', 'PhotoStreamController');
Run Code Online (Sandbox Code Playgroud)然后,我启动棘轮服务器.
sudo php artisan latchet:listen
当照片上传后,我可以运行以下代码将更新推送到正在收听我主题的客户端(PhotoStream/client1在本例中):
// Create the object, save it to db and then publish it to my websockets
$photo = new Photo;
$photo->location = 'path/to/file';
$photo->save();
// Publish it through my websocket clients. (push from server).
Latchet::publish('PhotoStream/client1', array('msg' => $photo->toArray() ));
Run Code Online (Sandbox Code Playgroud)
这段代码都有效,但是在更新的情况下.我的问题如下:
我该如何处理客户端的初始化?
这两个选项中的后一个对我来说似乎是最好的选择,但我真的不知道如何以一种好的方式实现它.
如何在不使用命令行的情况下遵循此类教程?
http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/
我需要能够使用websockets但拥有共享服务器并且无法访问命令行.
我不能简单地从浏览器运行脚本,如果我关闭浏览器/断开互联网连接时脚本将停止运行.
我试图连接到由PHP创建的安全websocket,但由于某种原因它不起作用.证书文件对于PHP是可读的.
到目前为止,这是我的代码(PHP方面;为简单起见,剥离代码):
$context = stream_context_create();
stream_context_set_option($context, 'ssl', 'allow_self_signed', false);
stream_context_set_option($context, 'ssl', 'verify_peer', true);
stream_context_set_option($context, 'ssl', 'peer_name', 'example.com');
stream_context_set_option($context, 'ssl', 'CN_match', 'example.com');
stream_context_set_option($context, 'ssl', 'SNI_enabled', true);
stream_context_set_option($context, 'ssl', 'local_cert', '/path/to/ssl/cert/example.com');
stream_context_set_option($context, 'ssl', 'local_pk', '/path/to/ssl/private/example.com');
$serverSocket = stream_socket_server(
'tls://example.com:8090',
$errNo,
$errStr,
\STREAM_SERVER_BIND | \STREAM_SERVER_LISTEN,
$context
);
$client = stream_socket_accept($serverSocket);
// send initial websocket connection stuff
$request = socket_read($client, 5000);
preg_match('#Sec-WebSocket-Key: (.*)\r\n#', $request, $matches);
$key = base64_encode(pack(
'H*',
sha1($matches[1] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
));
$headers = "HTTP/1.1 101 Switching Protocols\r\n";
$headers .= "Upgrade: websocket\r\n";
$headers …Run Code Online (Sandbox Code Playgroud) 我正在用PHP开发一个简单的websocket服务器.我知道有很多现有的实现,但我想自己做,以便更好地学习协议.我设法做握手罚款,我的客户连接到服务器.我还设法解码来自客户端的数据,但我发送回消息时遇到问题.客户端收到我的响应后会断开连接.Firefox说The connection to ws://localhost:12345/ was interrupted while the page was loading..
我用这个答案作为指导.
这是我的数据包装代码:
private function wrap($msg = ""){
$length = strlen($msg);
$this->log("wrapping (" . $length . " bytes): " . $msg);
$bytesFormatted = chr(129);
if($length <= 125){
$bytesFormatted .= chr($length);
} else if($length >= 126 && $length <= 65535) {
$bytesFormatted .= chr(126);
$bytesFormatted .= chr(( $length >> 8 ) & 255);
$bytesFormatted .= chr(( $length ) & 255);
} else {
$bytesFormatted .= chr(127);
$bytesFormatted …Run Code Online (Sandbox Code Playgroud) 我使用javascript连接websocket:
<script>
var socket;
var host = "ws://localhost:8000/socket/server/startDaemon.php";
var socket = new WebSocket(host);
</script>
Run Code Online (Sandbox Code Playgroud)
我收到了错误:
无法与服务器建立连接
var host = "ws://localhost:8000/socket/server/startDaemon.php";
var socket = new WebSocket(host);
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
注意:我在mozilla中启用了websocket以支持Web套接字应用程序.当我在chrome中运行时出现错误:
can't establish a connection to the server at ws://localhost:8000/socket/server/startDaemon.php. var socket = new WebSocket(host);
Run Code Online (Sandbox Code Playgroud) 我开发了一个多人纸牌游戏,因此使用了websocket.为了在php中实现websocket,我使用了这个库
我把它放到我的ubuntu服务器上,该程序在Chrome浏览器和Firefox中运行正常(前端是使用Javascript实现的).使用Edge Browser时,会出现一条错误,指出"ReferenceError:WebSocket未定义".但在互联网上我已经读过Edge应该通常支持websockets.
我已经检查过,文档模式是否是另一个IE版本,但它也设置为edge.确切的版本是11.0.0600.18537.
以下是我的代码(虽然我不认为它是一个问题,因为它在其他浏览器中工作)
connectToSocket=function() {
var host = "ws://[host]:9000/echobot";
try
{
socket = new WebSocket(host);
console.log('WebSocket - status ' + socket.readyState);
...
}
catch(ex){
console.log('some exception : ' +ex);
}
Run Code Online (Sandbox Code Playgroud)
有人知道Edge有什么问题吗?
编辑: 我试过这个phpwebsocket:http://www.wilky.it/Shared/phpwebsocket.zip它可以在Firefox中运行,但我的问题仍然存在:如何让websockets与Chrome 17中的php服务器配合使用?
我在这里按照教程:http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/
似乎客户端连接,然后立即断开连接.我在控制台中发现了这个错误:
WebSocket握手期间出错:缺少"Sec-WebSocket-Accept"标头
我正在我的WAMP localhost上的Chrome 17.0.963.56中尝试启用php_sockets扩展.
我在某处看到Chrome已经改变了它所支持的内容,但它没有深入探讨如何修复它.我希望有人能指引我.(我是websockets的新手).
服务器:
{PATH}> php startDaemon.php
2012-02-20 07:02:51系统:创建套接字资源ID#7
2012-02-20 07:02:51系统:Socket绑定到localhost:8000.
2012-02-20 07:02:51系统:开始收听Socket.
2012-02-20 07:03:01 WebSocket:资源ID#8已连接!
2012-02-20 07:03:01 WebSocket:请求握手......
2012-02-20 07:03:01 WebSocket:握手......
2012-02-20 07:03:01 WebSocket:完成握手......
2012-02-20 07:03:01 WebSocket:资源ID#8已断开连接!
客户:
套接字状态:0
套接字状态:3(已关闭)
我正在使用Yii框架并OpenTok API与之集成PUSHER API.当我厌倦了创建PUSHER事件实例时,就建立了连接.但几秒钟后,连接自动关闭,并给出错误,例如:Pusher:Error:{"type":"WebSocketError","error":{}}.我正在尝试推送库版本1.11.2和1.12(但同时)进行测试.
控制台日志:
Pusher : State changed : initialized -> connecting
Firefox can't establish a connection to the server at ws://ws.pusherapp.com/app/?protocol=5&client=js&version=1.1....
Pusher : State changed : connecting -> connected
Pusher : State changed : connecting -> disconnected
Run Code Online (Sandbox Code Playgroud)
我已经检查了telnet ws.pusherapp.com 443,80和843,但我得到的是Connection被外国主机关闭.
是否有任何方法或东西我缺乏调试或测试?需要一些帮助来避免这种警告.
我用过
chrome版本23.0.1271.97
Ubuntu 12.04
Firefox 18.0
我安装了node.js v0.8.22并尝试在Windows 7中安装socket.io 32bit Socket.io安装连续失败.控制台日志是:
C:\Program Files\nodejs>npm install socket.io
npm http GET https://registry.npmjs.org/socket.io
npm http GET https://registry.npmjs.org/socket.io
npm http 200 https://registry.npmjs.org/socket.io
npm http GET https://registry.npmjs.org/socket.io/-/socket.io-0.9.13.tgz
npm http 200 https://registry.npmjs.org/socket.io/-/socket.io-0.9.13.tgz
npm ERR! Error: EPERM, mkdir 'C:\Program Files\nodejs\node_modules\socket.io'
npm ERR! { [Error: EPERM, mkdir 'C:\Program Files\nodejs\node_modules\socket.io
']
npm ERR! errno: 50,
npm ERR! code: 'EPERM',
npm ERR! path: 'C:\\Program Files\\nodejs\\node_modules\\socket.io',
npm ERR! fstream_type: 'Directory',
npm ERR! fstream_path: 'C:\\Program Files\\nodejs\\node_modules\\socket.io',
npm ERR! fstream_class: 'DirWriter',
npm ERR! fstream_stack:
npm ERR! [ 'DirWriter._create (C:\\Program …Run Code Online (Sandbox Code Playgroud) phpwebsocket ×10
websocket ×9
php ×6
html5 ×2
apache ×1
javascript ×1
laravel ×1
laravel-4 ×1
node.js ×1
pusher ×1
ratchet ×1
serversocket ×1
socket.io ×1
ssl ×1
web-services ×1
zeromq ×1