I am building a chat and all works fine.
我正在建立一个聊天,一切正常。
When person A types a message in a textarea and clicks the SEND button of an HTML form, PHP sends it to the server.
当人A在textarea中键入消息并单击HTML表单的SEND按钮时,PHP会将其发送到服务器。
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect textarea content
// inserted (appended) to mySQL table.
// collect chat content in mySQL table
// display chat content in a <div>
}
?>
(I am not posting the PHP code because it's irrelevant to the problem)
(我不发布PHP代码,因为它与问题无关)
Person B gets the message because of a javascript setInterval that triggers a function that grabs the chat content from the database via AJAX and then displays the chat content for person B in a div.
Person B获取消息是因为javascript setInterval触发了一个函数,该函数通过AJAX从数据库中获取聊天内容,然后在div中显示人B的聊天内容。
setInterval(refreshChat, 1000);
function refreshChat() {
$.ajax({
url: "http://eyesonpi.com/_/php/chat.php",
type: "post",
success: function(data) {
$("#chatWindow").html(data);
},
error: function() {
$("#chatWindow").prepend("Error");
}
});
}
When I tried it on my iPhone, running IOS 10, the AJAX call fails. IOS 10 has a problem with AJAX. https://forums.developer.apple.com/thread/64526
当我在iPhone上运行IOS 10时,AJAX调用失败。 IOS 10存在AJAX问题。 https://forums.developer.apple.com/thread/64526
I tried with plain Javascript (xmlHttpRequest) and with jQuery.
我尝试使用普通的Javascript(xmlHttpRequest)和jQuery。
I tried the chat window of my hosting company from my iPhone and their chat works. I don't know where to begin with a workaround. How can person B get the message from person A without reloading the page? Any suggestions?
我从我的iPhone和他们的聊天工作尝试了我的托管公司的聊天窗口。我不知道从哪里开始解决方法。 B人如何在没有重新加载页面的情况下从A人那里获取消息?有什么建议?
Thank you.
2 个解决方案
#1
2
You can try to use websockets for the chat. Or long pooling technology which allows connection to be persistent between server and client(browser or mobile app). if you choose websockets. Then you can try to use on server side: Workerman or Ratchet.Both, perfect do their job. All modern browsers natively support websocket, or you can use popular libraries like sockJS for cross-browser support.
您可以尝试使用websockets进行聊天。或者长池技术,允许连接在服务器和客户端(浏览器或移动应用程序)之间保持持久性。如果你选择websockets然后你可以尝试在服务器端使用:Workerman或Ratchet.Both,完美地完成他们的工作。所有现代浏览器本身都支持websocket,或者您可以使用像sockJS这样的流行库来提供跨浏览器支持。
#2
0
Try to replace setInterval
to setTimeout
behaviour, otherwise previous request will be killed if the next is submitted
尝试将setInterval替换为setTimeout行为,否则如果提交下一个请求将被杀死
setTimeout(refreshChat, 0);
function refreshChat() {
$.ajax({
url: "http://eyesonpi.com/_/php/chat.php",
type: "post",
success: function (data) {
$("#chatWindow").html(data);
},
error: function () {
$("#chatWindow").prepend("Error");
}
}).always(function () {
setTimeout(refreshChat, 1000);
});
}
#1
2
You can try to use websockets for the chat. Or long pooling technology which allows connection to be persistent between server and client(browser or mobile app). if you choose websockets. Then you can try to use on server side: Workerman or Ratchet.Both, perfect do their job. All modern browsers natively support websocket, or you can use popular libraries like sockJS for cross-browser support.
您可以尝试使用websockets进行聊天。或者长池技术,允许连接在服务器和客户端(浏览器或移动应用程序)之间保持持久性。如果你选择websockets然后你可以尝试在服务器端使用:Workerman或Ratchet.Both,完美地完成他们的工作。所有现代浏览器本身都支持websocket,或者您可以使用像sockJS这样的流行库来提供跨浏览器支持。
#2
0
Try to replace setInterval
to setTimeout
behaviour, otherwise previous request will be killed if the next is submitted
尝试将setInterval替换为setTimeout行为,否则如果提交下一个请求将被杀死
setTimeout(refreshChat, 0);
function refreshChat() {
$.ajax({
url: "http://eyesonpi.com/_/php/chat.php",
type: "post",
success: function (data) {
$("#chatWindow").html(data);
},
error: function () {
$("#chatWindow").prepend("Error");
}
}).always(function () {
setTimeout(refreshChat, 1000);
});
}