Answer the question
In order to leave comments, you need to log in
SockJS, node.js, what if php doesn't have time?
I play with real time and made a simple link, actually an example of what will happen in the future:
Our client:
<script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
<div id="content"></div>
<script type="text/javascript">
var content = document.getElementById('content');
var sock = new SockJS('http://127.0.0.1:9999/echo');
sock.onopen = function() {
console.log('open');
};
sock.onmessage = function(e) {
content.innerHTML = e.data;
};
sock.onclose = function() {
console.log('close');
};
</script>
var http = require('http');
var sockjs = require('sockjs');
var http = require('http');
var options = {
host: 'portf2.ru',
path: '/poling/server.php'
};
var echo = sockjs.createServer();
echo.on('connection', function(conn) {
setInterval(function(){
var req = http.get(options, function(res) {
//console.log('STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers));
// Buffer the body entirely for processing as a whole.
var bodyChunks = [];
res.on('data', function(chunk) {
// You can process streamed parts here...
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
var response = JSON.parse(body);
conn.write(response.date);
})
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
});
}, 1000);
conn.on('data', function(message) {
conn.write(message);
});
conn.on('close', function() {});
});
var server = http.createServer();
echo.installHandlers(server, {prefix:'/echo'});
server.listen(9999, '0.0.0.0');
<?php
$objDateTime = new DateTime('NOW');
$date = $objDateTime->format('H:i:s');
$ar = ['date'=>$date];
echo json_encode($ar);
Answer the question
In order to leave comments, you need to log in
What happens if JS fails? From what you described, the cost of data delivery will be ten times higher than the cost of computing. To begin with, it is worth understanding that setInterval does not guarantee that the function will be called exactly in one second, there will also be fluctuations there due to the single-threaded JS. How to deal with this, I won’t tell you right away, it all depends on the application logic.
10,000 simultaneous requests to the server (just individual requests, not via WebSockets) can bring down a small server. That's what load testing is for.
TCP guarantees no data loss, in fact, in real-time things, TCP is used only if UDP gives a lot of packet loss (for example, UDP traffic is cut or something like that). That is, when the server sends a packet, it buffers it until the client acknowledges its receipt. If the server does not receive confirmation via ICMP that the packet has been sent within a certain time, it starts sending the entire buffer again. When the buffer is full, you can no longer send new packets until at least half is free, as far as I remember. In realtime, you should not be confused by the situation with packet loss (if the data is placed in one packet, of course), since they do not have much time to carry "actual data". it's easier to send the next packet. than to resend data that is no longer relevant.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question