Answer the question
In order to leave comments, you need to log in
Websocket + Vk Long Poll (PHP+Ratchet)?
I'm trying to implement instant messaging (in real time) from VK to long poll, after reading the article 7vn.ru/blog/2012/09/09/vkontakte-async I tried to do something similar to PHP.
When I run php server.php then websocket data exchange occurs, but when I run long polling vk then I have a websocket connection constantly pending , somehow blocking the websocket connection. But at the same time, vk long poll itself works, I see that updates from VK are coming to the console, Code:
<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Session\SessionProvider;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;
use getjump\Vk\Wrapper\LongPoll;
use getjump\Vk\Core;
use getjump\Vk\Model\User as User;
// Make sure composer dependencies have been installed
require __DIR__ . '/vendor/autoload.php';
chdir('../');
require 'vendor/autoload.php';
class Messenger extends getjump\Vk\Wrapper\LongPoll
{
public function __construct($vk) {
parent::__construct($vk);
}
public function longPolling($connect) {
$server = $this->getServerData();
$initial = $this->getConnectionInfo($server);
$user = new User($this->vk);
$userMap = [];
$userCache = [];
/**
* @param $id
* @return Model\User
*/
$fetchData = function ($id) use ($user, &$userMap, &$userCache) {
if (!isset($userMap[$id])) {
$userMap[$id] = sizeof($userCache);
$userCache[] = $user->get($id)->response->get();
}
return $userCache[$userMap[$id]];
};
while ($data = $this->guzzle->get($initial)->json(['object' => true])) {
$server->ts = $data->ts;
$initial = $this->getConnectionInfo($server);
if ($data->updates) {
$connect->send(var_dump($data->updates));
}
}
}
}
class myApp implements MessageComponentInterface
{
protected $clients;
protected $vk;
protected $vkPoll;
protected $sessid;
protected $session;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function getSession($sessionId) {
// Apache server's get_session_info.php
// Note: restrict access to this path so that remote users can't dump
// their own session data.
$opts = array('http' => array('method' => 'GET', 'header' => "Cookie: PHPSESSID=$sessionId\r\n"));
$context = stream_context_create($opts);
$json = file_get_contents('http://site.com/get_session_info.php', false, $context);
$session_data = json_decode($json);
return $session_data;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
$this->sessid = $conn->WebSocket->request->getQuery() ['sessid'];
$this->session = $this->getSession($this->sessid);
// var_dump($this->session);
if ($this->session) {
$this->vk = getjump\Vk\Core::getInstance()->apiVersion('5.34')->setToken((isset($this->session->vk_msg_token) ? $this->session->vk_msg_token: $this->session->vk_closed_token) );
$this->messanger = new Messenger($this->vk);
}
// Вот в этом месте блокирует websocket
// $this->messanger->longPolling($conn);
}
public function onMessage(ConnectionInterface $from, $msg) {}
public function onClose(ConnectionInterface $conn) {
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, \Exception $e) {
$conn->close();
}
}
// Run the server application through the WebSocket protocol on port 8080
$server = new Ratchet\App('SERVER_IP_ADDRESS', 8080, '0.0.0.0');
$server->route('/chat', new myApp, array('*'));
$server->run();
jQuery(document).ready(function($) {
var sessid = $('.sessid').val();
var conn = new WebSocket('ws://SERVER_IP_ADDRESS:8080/chat?sessid='+ sessid);
conn.onopen = function(e) {
conn.send('New User connected');
};
});
Answer the question
In order to leave comments, you need to log in
I found out that you can’t run an eternal loop in one process, you need to run VK long poll in one process, and run Websocket in the second one, and communicate via tcp protocol using zeromq.org , an implementation example is socketo.me/docs/push
But two questions arise :
1) how to make many users listen to their messages with their access_token, through "their" process or daemon running.
2) if, for example, 1000 eternal cycles are launched - will the server not lie down, or can you go the other way?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question