简体   繁体   English

通过 WAMP 服务器使 Socket.IO 从本地主机工作

[英]Making Socket.IO work from localhost through WAMP Server

I'm very new to using websocket.我对使用 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).我了解 PHP、MySQL、HTML、CSS 和 JS(或 jQuery)。

My project is already started in PHP and MySQL but I need to add some "live" part.我的项目已经在 PHP 和 MySQL 中启动,但我需要添加一些“实时”部分。 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. websocket 应用程序的目标是检查 MySQL 数据库中表中的任何更改,并在适用时通知必要的客户端。 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.现在,我已经能够遵循 Socket.IO 的几个教程,并且我在官方网站上的聊天示例工作正常。 (With Express, Socket.IO and Node.JS) (使用 Express、Socket.IO 和 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.所以如果我理解正确的话,index.js 应该是我的应用服务器,而 index.php 是我的网站。

Everything is default right now so I access the website at http://localhost/ .现在一切都是默认的,所以我通过http://localhost/访问网站。 I was access the tutorial one (/LiveUpdate/Index.html) at http://localhost:3000/ .我在http://localhost:3000/访问教程一 (/LiveUpdate/Index.html)。

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:在过去的几个小时里,我尝试了很多东西,但 Vivaldi (Chrome) 总是返回这个:

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)在我的 index.php 中,我有:(重要代码的精简版)

<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:在 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你的 Socket.IO JavaScript 将在 :3000 端口

    <script src="http://localhost:3000/socket.io/socket.io.js"></script>
  2. Also be sure your server is running.还要确保您的服务器正在运行。 You can use forever https://github.com/foreverjs/forever你可以永远使用https://github.com/foreverjs/forever

forever start /path/index.js永远开始/path/index.js

It works like a daemon它像守护进程一样工作

EDIT:编辑:


For a complete working index.js: 对于完整的工作 index.js:

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
    $(function () {
        var socket = io.connect('http://localhost:3000');
    }
</script>

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>

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

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