[英]Socket.io message event not emitting from client to server
I'm stuck in a rut here and would love some help.我在这里陷入困境,希望得到一些帮助。 I'm new to Node.js, Socket.io and Express - I just started learning today, but I've made fast progress.我是 Node.js、Socket.io 和 Express 的新手 - 我今天刚刚开始学习,但我取得了快速的进步。 I'm trying to build a simple little webchat application, but I've run into a problem.我正在尝试构建一个简单的小型网络聊天应用程序,但遇到了一个问题。 I have set up a form with a textbox that when submit, gets sent to the server, and the server logs the value in the console.我已经设置了一个带有文本框的表单,当提交时,它会被发送到服务器,服务器会在控制台中记录该值。 The only problem is it is not logging it to the console.唯一的问题是它没有将其记录到控制台。
The server and the client connect just fine (I have a console.log("Client connected...") message when a client connects), but the messages event emitter doesn't seem to work.服务器和客户端连接得很好(当客户端连接时,我有一个 console.log("Client connected...") 消息),但消息事件发射器似乎不起作用。 Am I using outdated code?我使用的是过时的代码吗? Invalid syntax?无效的语法? I've spent hours searching for an answer and haven't found one yet.我花了几个小时寻找答案,但还没有找到。
My application is divided into an app.js, index.html, and /public/default.css.我的应用程序分为app.js、index.html和/public/default.css。
App.js:应用程序.js:
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function(client) {
console.log("Client connected...");
client.on('messages', function(data) {
console.log(data);
});
});
app.use('/public', express.static(__dirname + '/public'));
app.get('/', function(request, response) {
response.sendfile(__dirname + "/index.html");
});
server.listen(3000);
console.log("Listening on port 3000");
index.html:索引.html:
<!DOCTYPE html>
<html>
<head>
<title>Chatapp</title>
<link rel="stylesheet" type="text/css" href="public/default.css"></link>
</head>
<body>
<h1 class="title">Chatapp</h1>
<div id="wrapper">
<div id="online">
</div>
<div id="body">
</div>
<div id="input">
<form id="chatform">
<input type="text" id="message"></input>
<input type="submit" id="send" value="Send!"></input>
</form>
</div>
<div class="clear"></div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
$(document).ready(function() {
var server = io.connect('http://localhost:3000');
$('#chatform').submit(function(e) {
e.preventDefault();
var message = $('#message').val();
socket.emit('messages', message);
});
});
</script>
</body>
</html>
/public/default.css: /public/default.css:
body {
margin: 0;
padding: 0;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}
#wrapper {
margin: 0 auto;
width: 940px;
height: 500px;
padding: 0;
border: 1px solid #000;
}
#online {
float: left;
width: 188px;
height: 480px;
padding: 10px;
border-right: 1px solid #000;
}
#body {
float: left;
width: 711px;
height: 419px;
padding: 10px;
border-bottom: 1px solid #000;
}
#input {
float: left;
height: 60px;
width: 731px;
}
.clear {
clear: both;
}
h1.title {
text-align: center;
}
input#message {
float: left;
margin: 0;
width: 640px;
height: 58px;
border: none;
font-size: 28px;
padding-left: 10px;
box-shadow: inset 1px 1px 3px rgba(0,0,0,0.3);
}
input#message:focus {
border: none;
outline: none;
box-shadow: inset 1px 1px 3px #55ba57, inset -1px -1px 3px #55ba57;
}
input#send {
float: left;
background: #55ba57;
color: #fff;
border: none;
height: 60px;
margin: 0;
width: 81px;
border-left: 1px solid #000;
}
input#send:hover {
background: #489949;
cursor: pointer;
}
input#send:active {
background: #1e7520;
}
You're changing the names and I think that's leading to some confusion.您正在更改名称,我认为这会导致一些混乱。 On the client side you're setting a variable called server, yet you're calling socket.emit().在客户端,您正在设置一个名为 server 的变量,但您正在调用 socket.emit()。 There should have been some error messages in your browser console which would help you track this down.您的浏览器控制台中应该有一些错误消息可以帮助您进行跟踪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.