Javascript Socket.io不会发出事件
我开始学习socket.io。 我从socket.io网站示例开始本教程 我已正确安装了所有内容,但我认为套接字无法在index.html中发出事件 任何人都可以帮忙 这是我的密码 index.js index.htmlJavascript Socket.io不会发出事件,javascript,node.js,sockets,socket.io,Javascript,Node.js,Sockets,Socket.io,我开始学习socket.io。 我从socket.io网站示例开始本教程 我已正确安装了所有内容,但我认为套接字无法在index.html中发出事件 任何人都可以帮忙 这是我的密码 index.js index.html <!doctype html> <html> <head> <title>Socket.IO chat</title> <style> * { margin: 0; padd
<!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; }
#messages { margin-bottom: 40px }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
console.log('sample');
return false;
});
socket.on('chat message', function(msg){
console.log('sample');
$('#messages').append($('<li>').text(msg));
});
});
</script>
</body>
</html>
请检查这个作为参考- io.toroom.emit'msg'msg,或elase socket.emit'hello',你能听到我说话吗?',1,2,'abc' 您在服务器上的“聊天信息”的Socket.中错误地将console.log键入consloe.log。这就是为什么它会崩溃,而不会发射 另外,在服务器和浏览器上使用最新版本的socket.io
在客户端使用socket.io-client,您可以在codecdn.socket.io/socket.io-1.2.0.js>中看到,您正在从cdn为socket.io提供服务。客户端上socket.io的版本应与服务器上使用的版本匹配。当前版本为2.0.3,但客户端上使用的是1.2.0
<!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; }
#messages { margin-bottom: 40px }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
console.log('sample');
return false;
});
socket.on('chat message', function(msg){
console.log('sample');
$('#messages').append($('<li>').text(msg));
});
});
</script>
</body>
</html>