【问题标题】:Socket.IO Chat not sending and/or recieving messagesSocket.IO Chat 不发送和/或接收消息
【发布时间】:2015-04-06 11:33:48
【问题描述】:

我遵循了 socket.io 聊天的教程: https://www.youtube.com/watch?v=pNKNYLv2BpQ

在教程视频中它工作正常,但我的根本不工作。服务器或客户端似乎都没有发送或接收消息

这是我的 app.js 在服务器端使用节点运行

var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);

server.listen(3000);

app.get('/', function(req, res) {
    res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function(socket) {
    socket.on('send message', function(data) {
        io.sockets.emit('new message', data);
    });
});

这是我的 index.html 客户端

<html>
<head>
<title>Chat with socket.io and node.js</title>
<style>
    #chat
    {
        height: 500px;
    }
</style>
</head>
<body>
<div id="chat"></div>
<form id="send-message">
    <input size="35" id="message"></input>
    <input type="submit"></input>
</form>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
    jQuery(function($) {
        var socket = io.connect();
        var $messageForm = $('#send-message');
        var $messageBox = $('#message');
        var $chat $('#chat');

        $messageForm.submit(function(e) {
            e.preventDefault();
            socket.emit('send message', $messageBox.val());
            $messageBox.val('');
        });

        socket.on('new message', function(data) {
            $chat.append(data + "<br/>");
        });
    });
</script>

我想这是一个我忽略的简单错误,但我现在很困惑,有什么想法吗???另外,如果您需要更多信息,请说出来:)

【问题讨论】:

  • 试试这样吧,var io = require('socket.io')(server);
  • @NLN 我在尝试您的解决方案时得到了这个:/home/snapper26/Desktop/chat/app.js:4 io = require('socket.io')(server); ^ TypeError: object is not a function at Object. (/home/snapper26/Desktop/chat/app.js:4:27) at Module._compile (module.js:456:26) at Object.Module。 _extensions..js (module.js:474:10) 在 Module.load (module.js:356:32) 在 Function.Module._load (module.js:312:12) 在 Function.Module.runMain (module. js:497:10) 在启动时 (node.js:119:16) 在 node.js:906:3

标签: javascript jquery html node.js socket.io


【解决方案1】:

创建一个 index.html 文件

index.html

   <!doctype html>
<html>
  <head>
    <title>Socket.IO 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>
  </body>
</html>


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

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('index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
socket.on('chat message', function(msg){
    console.log('message: ' + msg);
  });
});

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

For more information click here

【讨论】:

    猜你喜欢
    • 2016-09-30
    • 1970-01-01
    • 2015-01-16
    • 2021-06-30
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    相关资源
    最近更新 更多