NodeJS - 收到来自url的参数时发送套接字

时间:2017-01-29 07:04:34

标签: javascript node.js socket.io

我正在尝试在收到url中的get请求?server 时使用socket.io发送套接字。我正在加载按钮,这些按钮将从node.js服务器发送的json文件重定向页面。

收到get请求后,会向服务器发送一个套接字。问题是在服务器端,套接字由于某种原因没有收到。

的index.html

    <script src="jquery.js"></script>
    <script src="jquery-ui.js"></script>
    <script src="jquery-ui.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Bungee|Roboto" rel="stylesheet">

    <div id="channels" style="height: 100%; width: 100%; position: absolute; top: 0%; left: 0%; background-color: #000;"></div>

    <style>
        html {
            color: #fff;
        }
    </style>

    <script>

function getParameterByName(name, url) {
    if (!url) {
      url = window.location.href;
    }
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

    var socket = io();

    socket.on("server", function(server){
        console.log(server);
        $("#channels").append("<a href='?server="+server+"'><h1>"+server+"</h1></a>");
    });

    var server = getParameterByName("server");
            console.log("Connecting to",server);
            socket.emit("connect", server);

    </script>

server.js

   process.stdout.write('\033c'); // Clear the console on startup

var
   express = require("express"),
   app = express(),
   http = require("http").Server(app),
   io = require("socket.io")(http),
   path = require("path"),
   colorworks = require("colorworks").create();

      http.listen(8080, function(){
         console.log("server is running on port 8080");
      });

            app.use(express.static(__dirname + "/public2"));

            app.get("/", function(request, response){
                response.sendFile(__dirname + "/public2/index.html");
                console.log("Served file to client");
            });

    var database = require("./json/database_original.json");
    var number_servers = Object.keys(database.servers).length;

io.on("connection", function(socket){

    for (var i = 0; i < number_servers; i++){
        socket.emit("server", database.servers[i].serverJOIN);
        console.log("Server name sent: " + database.servers[i].serverJOIN);
    }

    socket.on("connect", function(server){
        console.log("Socket received: " + server);
    });

});

database.json

    {
        "servers": [
            {
                "serverNAME": "server1",
                "serverJOIN": "@server1"
            },
            {
                "serverNAME": "server2",
                "serverJOIN": "@server2"
            },
            {
                "serverNAME": "server3",
                "serverJOIN": "@server3"
            }
        ]
    }

1 个答案:

答案 0 :(得分:0)

您可能希望使用express-io,因为这是专门为此制作的,或者如果您正在构建实时Web应用程序,请尝试使用已集成了socketIO的sailsjs。

https://stackoverflow.com/a/23282360/4394814

请参阅此内容,这可能会对您有所帮助