简体   繁体   English

将Laravel(PHP)与socket.io混合

[英]Mixing Laravel(PHP) with socket.io

We have a Laravel project, however, some parts of the app do a lot of polling so we decided to introduce socket.io to the app. 我们有一个Laravel项目,但是,该应用程序的某些部分进行了大量轮询,因此我们决定将socket.io引入该应用程序。 Laravel rests on Apache which listens on port 80 (we use jQuery and Bootstrap in the front end). Laravel依赖于Apache,后者侦听端口80(我们在前端使用jQuery和Bootstrap)。 Our local test hostname is test.localhost.com. 我们的本地测试主机名是test.localhost.com。 The node server is just a server we created that listens on port 1000, with express and socket.io installed. 节点服务器只是我们创建的服务器,它侦听端口1000,并安装了express和socket.io。

In our Laravel page, we have the following include: <script src="//localhost:1000/socket.io/socket.io.js"></script> . 在Laravel页面中,我们包括以下内容: <script src="//localhost:1000/socket.io/socket.io.js"></script> As a test, we have the following code in our front end: 作为测试,我们在前端有以下代码:

<script>
$(document).ready(function() {
    var socket = io();
    $("#msg").keyup(function(event) {
        if (event.keyCode === 13) {
            socket.emit($("#msg").val());
            $("#msg").val('');
        }
    });
});
</script>

However, when I do try to send a message, nothing is received on the node side, but in the Chrome console I notice the following: 但是,当我尝试发送消息时,节点端什么也没收到,但是在Chrome控制台中,我注意到以下内容:

GET http://test.localhost.com/socket.io?EIO=3&transport=polling&t=1413006253169-84 404 (Not Found) , with an error on line 2680 of socket.io.js. GET http://test.localhost.com/socket.io?EIO=3&transport=polling&t=1413006253169-84 404(未找到),在socket.io.js的第2680行出现错误。 I open up the file and go to line 2680 and see xhr.send(this.data); 我打开文件,转到第2680行,查看xhr.send(this.data); . What's going on? 这是怎么回事? Can I not mix the 2 code bases? 我不能混合使用两种代码库吗? And why is it trying to GET at test.localhost.com when in the import I'm using localhost:1000 (different host and different port)? 为什么在导入中我使用localhost:1000(不同的主机和不同的端口)时尝试在test.localhost.com上获取数据?

Ok I got it. 好,我知道了。 When you create your connection, socket.io assumes you're connecting to localhost:80. 创建连接时,socket.io假定您正在连接到localhost:80。 If not, then you can specify your server host explicitly: 如果不是,则可以显式指定服务器主机:

var socket = io('http://localhost:1000');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM