繁体   English   中英

Socket.IO/Node.JS执行

[英]Socket.IO/Node.JS execution

我是Node.JS和Socket.IO的新手,所以我决定选择一个简单的聊天客户端作为我的第一个项目。 据我所知,它的结构正确,但是每当我尝试运行index.js时,都会抛出object expected错误。 有人知道怎么回事吗? (如果有帮助,我正在Windows 8.1上运行)

码:

Index.html

<!doctype html>
<html>
  <head>
    <title>TNjs Chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      var socket = io();
      $('form').submit(function(){
        socket.emit('chat message', $('#m').val());
        $('#m').val('');
        return false;
      });
      socket.on('chat message', function(msg){
        $('#messages').append($('<li>').text(msg));
      });
    </script>
  </body>
</html>

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){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

package.json

{
  "name": "TNjsChat Client",
  "version": "0.0.1",
  "description": "A browser based chat system using Node.JS and socket.io, by Touka",
  "dependencies": {
    "express": "4.10.2",
    "socket.io": "1.2.0"
  }
}

您需要运行节点,而不仅仅是让Microsoft IE运行您的屏幕快照似乎显示的js文件。

确保node.exe在某个路径中,然后从index.js目录运行“ node index.js”,并确保正确安装了所需的所有模块(如Express),并且可以从index.js目录获得这些模块。

暂无
暂无

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

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