在Windows 7上为node.js安装socket io

发布于 2025-01-08 15:02:22 字数 3674 浏览 0 评论 0原文

当我运行节点服务器时,控制台显示“info - socket.io started”。但是,它永远不会检测到来自客户端的连接。我相信这是安装 socket.io 的问题,因为我运行的是 Windows 7 机器...

当我尝试从客户端连接到服务器时,我的服务器控制台指示“调试 - 提供静态内容/ lib/socket.io.js”。

有人对如何让套接字 io 从 Windows 7 计算机上的客户端成功连接有任何建议吗?

现在,我的 socket.io 文件夹位于以下目录中:

nodejs/node_modules/socket.io/

对于 node< 中的 tcp 服务器,我在服务器端上有以下代码/strong>:

var http = require('http'),
    sys = require('util'),
    fs = require('fs'),
    io = require('socket.io');

console.log('Creating server...');
var server = http.createServer(function(request, response) {
  response.writeHead(200, {
    'Content-Type': 'text/html'
  });

  var rs = fs.createReadStream(__dirname + '/template.html');
  sys.pump(rs, response);

});

var socket = io.listen(server);

socket.on('connection', function(client) {

  var username;

  console.log('New client connected.');
  client.send('Welcome to this socket.io chat server!');
  client.send('Please input your username: ');

  client.on('message', function(message) {
    if (!username) {
      username = message;
      client.send('Welcome, ' + username + '!');
      return;
    }

    socket.broadcast(username + ' sent: ' + message);
  });

});

server.listen(20000);

这是客户端的代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Chat</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
    <script type="text/javascript" src="http://localhost:20000/socket.io/lib/socket.io.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        var entry_el = $('#entry');
        var socket = new io.Socket('localhost', {port: 20000});
        socket.connect();
        console.log('connecting...');
        socket.on('connect', function() {
          console.log('connect');
        });
        socket.on('message', function(message) {
          var data = message.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
          $('#log ul').append('<li>' + data + '</li>');
          window.scrollBy(0, 1000000000000000);
          entry_el.focus();
        });

        entry_el.keypress(function(event) {
          if (event.keyCode != 13) return;
          var msg = entry_el.attr('value');
          if (msg) {
            socket.send(msg);
            entry_el.attr('value', '');
          }
        });

      });
    </script>
    <style type="text/css">
      body {
        background-color: #666;
        color: fff;
        font-size: 14px;
        margin: 0;
        padding: 0;
        font-family: Helvetica, Arial, Sans-Serif;
      }
      #log {
        margin-bottom: 100px;
        width: 100%;
      }
      #log ul {
        padding: 0;
        margin: 0;
      }
      #log ul li {
        list-style-type: none;
      }
      #console {
        background-color: black;
        color: white;
        border-top:1px solid white;
        position: fixed;
        bottom: 0;
        width: 100%;
        font-size: 18px;
      }
      #console input {
        width: 100%;
        background-color: inherit;
        color: inherit;
        font-size: inherit;
      }
    </style>
  </head>
  <body>
    <h1>Chat</h1>
    <div id="log"><ul></ul></div>
    <div id="console">
      <input type="text" id="entry" />
    </div>
  </body>
</html>

谢谢!

When I run the node server, the console indicates "info - socket.io started". However, it never detects a connection from the client side. I believe it is an issue with the installation of socket.io as I am running a Windows 7 machine...

When I attempt to connect to the server from the client side my server console indicates "debug - served static content /lib/socket.io.js".

Anyone have any suggestions how I can get socket io to succesfully connect from the client side on my Windows 7 machine?

Right now, I have the socket.io folder located in the following directory:

nodejs/node_modules/socket.io/

I have the following code on the server side for a tcp server in node:

var http = require('http'),
    sys = require('util'),
    fs = require('fs'),
    io = require('socket.io');

console.log('Creating server...');
var server = http.createServer(function(request, response) {
  response.writeHead(200, {
    'Content-Type': 'text/html'
  });

  var rs = fs.createReadStream(__dirname + '/template.html');
  sys.pump(rs, response);

});

var socket = io.listen(server);

socket.on('connection', function(client) {

  var username;

  console.log('New client connected.');
  client.send('Welcome to this socket.io chat server!');
  client.send('Please input your username: ');

  client.on('message', function(message) {
    if (!username) {
      username = message;
      client.send('Welcome, ' + username + '!');
      return;
    }

    socket.broadcast(username + ' sent: ' + message);
  });

});

server.listen(20000);

This is the code on the client side:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Chat</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
    <script type="text/javascript" src="http://localhost:20000/socket.io/lib/socket.io.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        var entry_el = $('#entry');
        var socket = new io.Socket('localhost', {port: 20000});
        socket.connect();
        console.log('connecting...');
        socket.on('connect', function() {
          console.log('connect');
        });
        socket.on('message', function(message) {
          var data = message.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");
          $('#log ul').append('<li>' + data + '</li>');
          window.scrollBy(0, 1000000000000000);
          entry_el.focus();
        });

        entry_el.keypress(function(event) {
          if (event.keyCode != 13) return;
          var msg = entry_el.attr('value');
          if (msg) {
            socket.send(msg);
            entry_el.attr('value', '');
          }
        });

      });
    </script>
    <style type="text/css">
      body {
        background-color: #666;
        color: fff;
        font-size: 14px;
        margin: 0;
        padding: 0;
        font-family: Helvetica, Arial, Sans-Serif;
      }
      #log {
        margin-bottom: 100px;
        width: 100%;
      }
      #log ul {
        padding: 0;
        margin: 0;
      }
      #log ul li {
        list-style-type: none;
      }
      #console {
        background-color: black;
        color: white;
        border-top:1px solid white;
        position: fixed;
        bottom: 0;
        width: 100%;
        font-size: 18px;
      }
      #console input {
        width: 100%;
        background-color: inherit;
        color: inherit;
        font-size: inherit;
      }
    </style>
  </head>
  <body>
    <h1>Chat</h1>
    <div id="log"><ul></ul></div>
    <div id="console">
      <input type="text" id="entry" />
    </div>
  </body>
</html>

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2025-01-15 15:02:22

您的socketio js应该位于以下路径中

http://{host:port}/socket.io/socket.io.js

在您的情况下,您需要包含如下内容

<script type="text/javascript" src="http://localhost:20000/socket.io/lib/socket.io.js"></script>

Your socketio js should be inside the following path

http://{host:port}/socket.io/socket.io.js

In your case you need to include as follows

<script type="text/javascript" src="http://localhost:20000/socket.io/lib/socket.io.js"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文