问题
I'm very new to using websocket. While I have a lot of experience in coding as a professional, I've never had to use socket for live update information.
I know my way around PHP, MySQL, HTML, CSS and JS (or jQuery).
My project is already started in PHP and MySQL but I need to add some "live" part. The goal of the websocket app would be to check for any change in the Table within the MySQL DB and notify the necessary client if applicable. I didn't wanted to "refresh" the page every so often as this was in my opinion a sloppy of doing things.
Now, I have been able to follow a couple of tutorial of Socket.IO and I got the chat example on the official website working fine. (With Express, Socket.IO and Node.JS)
The issue is now that I want to integrate this in my main website so I'm guessing I should be able to call it from my main website but it just doesn't work.
After searching for hours for a solution, I've heard that hierarchy was important so here is mine:
/www/
/www/Index.php
/www/LiveUpdate/
/www/LiveUpdate/index.js
So if I understood that right, index.js should be my app server while index.php is my website.
Everything is default right now so I access the website at http://localhost/. I was access the tutorial one (/LiveUpdate/Index.html) at http://localhost:3000/.
The tutorial one is working perfectly fine but I can'T connect in localhost. I've tried many thing in the past hours but Vivaldi (Chrome) always return this:
POST http://localhost/LiveUpdate/socket.io/?EIO=3&transport=polling&t=M6iMHf6 404 (Not Found)
In my index.php, I have: (Stipped down version of the important code)
<script src="/LiveUpdate/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost/:3000', {path: "/LiveUpdate/socket.io"});
</script>
and in index.js:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//app.get('/', function(req, res){
// res.sendFile(__dirname + '/index.html');
//});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
I appreciate any help. Best Regards,
回答1:
Your Socket.IO JavaScript will be on :3000 port
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
Also be sure your server is running. You can use forever https://github.com/foreverjs/forever
forever start /path/index.js
It works like a daemon
EDIT:
For a complete working index.js:
var express = require('express'),
http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);
io.on('connection', function(socket){
console.log('a user connected');
});
And inside your webpage:
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
$(function () {
var socket = io.connect('http://localhost:3000');
}
</script>
来源:https://stackoverflow.com/questions/48859633/making-socket-io-work-from-localhost-through-wamp-server