Socket.IO聊天不发送和/或接收消息

时间:2015-02-06 10:38:25

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

我已经关注了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>

我想这是一个简单的错误,我忽略了但是我现在很难过,任何想法?如果您需要其他信息,请说明:)

1 个答案:

答案 0 :(得分:0)

创建一个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