你好stackoverflow社区,我正在学习如何实现Pusher API http://pusher.com到这个简单的网络聊天应用程序。我正在按照视频教程正确执行每一步,但当我尝试发送消息时,它会在我的网络浏览器上正确显示,但不会在其他网络浏览器上显示或刷新。我将添加我的2个PHP文件,它们很短。集成Pusher到一个简单的PHP网络应用程序
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Pusher Messenger</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://js.pusher.com/3.1/pusher.min.js"></script>
<script>
// Enable pusher logging - don't include this in production
//Pusher.logToConsole = true;
var pusher = new Pusher('your pusher key here', {
encrypted: true
});
var channel = pusher.subscribe('channel_pusher');
channel.bind('new_message', function(response){
$('#sent_messages').append('<li>' + response.message + '</li>');
});
$(function(){
$('form').submit(function(){
$.post('ajax.php', { msj : $('#input_mensaje').val() }, function(response){
//funcion de callback
$('#sent_messages').append('<li>' + response.message + '</li>');
}, 'json');
return false;
});
});
</script>
</head>
<body>
<form action="" methor="post">
<input type="text" id="input_mensaje" />
<input type="submit" class="submit" value="Send" />
</form>
<ul id="sent_messages">
<!-- Sent messages will be shown here -->
</ul>
</body>
</html>
这是我的ajax.php文件:
<?php
require('lib/Pusher.php');
$options = array(
'encrypted' => true
);
$message = $_POST['msj'];
$pusher = new Pusher(
'code provided by pusher',
'code provided by pusher',
'code provided by pusher',
$options
);
$pusher->trigger(
'channel_pusher',
'new_message',
array('message' => $message)
);
echo json_encode(array('message' => $message));
?>
欢迎来到StackOverflow。听起来像你应该指定在哪个浏览器中正确地为你工作,并且在哪个浏览器中没有。还是其他任何浏览器,包括另一台PC上的相同浏览器和PC上的另一台浏览器? – YakovL
是的,你是对的,它不会刷新任何网页浏览器,主要问题是它没有显示实时消息。例如:我写了“Hello”,它会将其显示在我的网络浏览器上,但如果旁边有另一个网络浏览器,则不会显示任何消息。 –
我可以得到一些帮助吗? –