Socket.IO聊天不发送和/或接收消息
Socket.IO Chat not sending and/or recieving messages
我遵循了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>
我想这是一个我忽略了的简单错误,但我现在很困惑,有什么想法吗???此外,如果您需要其他信息,请这样说:)
创建一个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');
});
有关更多信息,请单击此处
相关文章:
- 通过node.js和socket.io在两个用户之间发送消息
- Socket.IO 客户端库提供“欢迎来到 socket.io”消息
- io.sockets.mit未向服务器发送消息
- 为什么我的消息没有用这个处理程序发送到Socket.io房间
- 在socket.io中从客户端发送广播消息
- 服务器到服务器套接字IO消息
- 发送消息时,Socket.io聊天重新加载页面
- Socket.IO聊天不发送和/或接收消息
- 如何确保客户端已收到通过socket.io发送的消息
- socket.io 客户端,不会向服务器发出消息
- Socket.io,给自己发消息
- socket.io 不记录控制台消息
- 向聊天室发送消息 socket.io 节点.js
- Socket.io 向某些套接字(体系结构)发出消息
- 向节点.js和 socket.io 中的单个套接字发送消息
- 用于 Block.io API 的 Websockets 给出错误消息
- Socket.io 无法连续发送两条消息
- 安装程序 prerender.io 仅从 PhantomJS 输出错误消息
- 为服务器中的每个客户端累积消息 Socket.io
- 向Socket发送消息.IO通过get请求