简体   繁体   中英

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 rests on Apache which listens on port 80 (we use jQuery and Bootstrap in the front end). Our local test hostname is test.localhost.com. The node server is just a server we created that listens on port 1000, with express and socket.io installed.

In our Laravel page, we have the following include: <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:

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. I open up the file and go to line 2680 and see 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)?

Ok I got it. When you create your connection, socket.io assumes you're connecting to localhost:80. If not, then you can specify your server host explicitly:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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