通过WAMP Server从本地主机使Socket.IO工作 [英] Making Socket.IO work from localhost through WAMP Server

查看:144
本文介绍了通过WAMP Server从本地主机使Socket.IO工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用websocket非常陌生.尽管我在编码方面有丰富的经验,但是我从来不需要使用套接字来获取实时更新信息.

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.

我知道我对PHP,MySQL,HTML,CSS和JS(或jQuery)的了解.

I know my way around PHP, MySQL, HTML, CSS and JS (or jQuery).

我的项目已经在PHP和MySQL中启动,但是我需要添加一些实时"部分. websocket应用程序的目标是检查MySQL数据库中表中的任何更改,并在适用时通知必要的客户端.我不想经常刷新页面,因为我认为这样做很草率.

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.

现在,我已经能够按照Socket.IO的几个教程进行操作,并且在官方网站上可以正常使用该聊天示例. (使用Express,Socket.IO和Node.JS)

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

因此,如果我理解正确,那么index.js应该是我的应用服务器,而index.php是我的网站.

So if I understood that right, index.js should be my app server while index.php is my website.

现在所有内容都是默认设置,因此我可以访问 http://localhost/的网站. 我正在 http://localhost:3000/中访问教程一(/LiveUpdate/Index.html).

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/.

本教程可以很好地运行,但是我无法在localhost中连接.在过去的几个小时里,我尝试了很多事情,但Vivaldi(Chrome)总是返回以下内容:

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)

在我的index.php中,我有:(重要代码的精简版)

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>

以及在index.js中:

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. 您的Socket.IO JavaScript将位于:3000端口

  1. Your Socket.IO JavaScript will be on :3000 port

<script src="http://localhost:3000/socket.io/socket.io.js"></script>

  • 还请确保您的服务器正在运行.您可以永久使用 https://github.com/foreverjs/forever

    永远启动/path/index.js

    forever start /path/index.js

    它像守护程序一样工作


    要获取完整的index.js文件,请执行以下操作:


    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');
    });
    

    在您的网页内:

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

    这篇关于通过WAMP Server从本地主机使Socket.IO工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆