[英]Socket.io event not firing
Here is the full code :- 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>
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chatIN', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chatOUT', function(msg){
$('#messages').append('<li><span>'+msg+'</span></li>');
});
});
</script>
</body>
</html>
And here is my index.js 这是我的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.sockets.on('connection', function(socket){
socket.on('chatIN', function(msg){
console.log('message:'+ msg);
socket.emit('chatOUT:'+ msg);
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
I am facing a problem while trying out a socket.io beginner's tutorial, I followed this tutorial https://socket.io/get-started/chat/ 我在尝试socket.io初学者教程时遇到问题,我遵循了该教程https://socket.io/get-started/chat/
Everything seems to be okay till the very last thing, when they build a chat app, and when you hit submit in textbox, it updates the contents of the element. 直到最后一件事,一切都似乎还可以,当他们构建一个聊天应用程序时,当您在文本框中单击“提交”时,它将更新该元素的内容。
I can see the messages getting printed in the node js console, but nothing in chrome's console. 我可以在node js控制台中看到消息被打印出来,但是在chrome的控制台中什么都没看到。 I also tried debugging it using developer options, but the control just never goes in this method in the index.html.
我也尝试使用开发人员选项对其进行调试,但该控件从未在index.html中采用此方法。
socket.on('chat message', function(msg){
Can someone please help ? 有人可以帮忙吗? I am really new to socket.io and nodejs.
我真的是socket.io和nodejs的新手。 Thanks
谢谢
You are sending the wrong message from the server. 您正在从服务器发送错误消息。
On the server, change this: 在服务器上,更改此:
socket.emit('chatOUT:'+ msg);
to this: 对此:
socket.emit('chatOUT', msg);
Your client code is listening for the chatOUT
message so that's the exact message name need to send. 您的客户代码正在侦听
chatOUT
消息,因此这是需要发送的确切消息名称。 Here's the matching client code: 这是匹配的客户端代码:
socket.on('chatOUT', function(msg){...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.