我通过创建实时聊天来尝试socket.io,并在提交消息时页面自动刷新,这里是标记和客户端JS(index.html):
<body>
<h1>Open chat</h1>
<form id="send_message">
<div class="chat">
</div>
<hr />
<input size="28" type="text" id="message" />
<input type="submit" value="send" />
</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 $message_form = $('#send_message');
var $message_box = $('#message');
var $chat = $('#chat');
$message_form.submit(function(e){
e.preventDefault();
socket.emit('send message', $message_box.val());
$message_box.val('');
})
});
socket.on('new message', function(data){
$chat.append(data+'<br />');
});
</script>
</body>
这是服务器端js(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);
});
});
我知道你们不需要这个,但这是package.json:
{
"name":"test_chat_application",
"version":"0.0.1",
"private":"true",
"dependencies":{
"socket.io":"1.4.8",
"express":"4.14.0"
}
}
基本上我认为问题是页面刷新! 请帮忙..
答案 0 :(得分:0)
我认为您应该添加return false
以防止表单“真正”提交(从而刷新页面)。我认为提交事件不是冒泡的,所以e.preventDefault在这种情况下不起作用。
$message_form.submit(function(e){
e.preventDefault();
socket.emit('send message', $message_box.val());
$message_box.val('');
return false;
})
答案 1 :(得分:0)
<div class="chat"></div> <!-- this is class -->
// But you are using
var $chat = $('#chat');
同时将chat
容器移到form#send_message
之外并使用document.ready
<div class="chat"></div>
<hr />
<form id="send_message">
<input size="28" type="text" id="message" />
<input type="submit" value="send" />
</form>
<script>
$(document).ready(function() {
var socket = io.connect();
var $message_form = $('#send_message');
var $message_box = $('#message');
var $chat = $('#chat');
$message_form.submit(function(e){
e.preventDefault();
socket.emit('send message', $message_box.val());
$message_box.val('');
});
socket.on('new message', function(data){
$chat.append(data+'<br />');
});
});
</script>
当你使用socket(哪种ajax)时 - 你放弃了表单元素的所有好处,所以根本就不要使用它
<div class="chat"></div>
<hr />
<input size="28" type="text" id="message"/>
<button id="send_message">send</button>
<script>
$(document).ready(function() {
var socket = io.connect();
var $message_form = $('#send_message');
var $message_box = $('#message');
var $chat = $('#chat');
$message_form.click(function(){
socket.emit('send message', $message_box.val());
$message_box.val('');
});
socket.on('new message', function(data){
$chat.append(data+'<br />');
});
});
</script>
答案 2 :(得分:0)
你必须抛弃表单,更新一些东西(比如var socket = io()而不是var socket = io.connect()和stuff)并使用onclick事件而不是表单提交来处理你的发送按钮。那样你就不会刷新了。
无论如何,更新的server.js:
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(3000);
app.use(express.static('./'));
app.get('/',function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection',function(socket){
console.log('SOMEONE CONNECTED:', socket.id);
socket.on('send', function(data){
console.log(data);
io.sockets.emit('new', data);
});
});
的index.html
<body>
<h1>Open chat</h1>
<div class="chat">
<ul id="messages">
</ul>
</div>
<hr />
<input size="28" type="text" id="message" />
<input type="button" onclick="send()" value="send" />
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
var socket = io();
function send(){
var message = document.getElementById("message").value;
socket.emit("send", { "message" : message });
document.getElementById("message").value = "";
}
socket.on("new", function(data){
console.log("NEW MESSAGE : ", data);
var chatMessage = document.createElement("li");
chatMessage.innerHTML = data.message;
document.getElementById("messages").appendChild(chatMessage)
});
</script>
试试这个,让我知道它是否有效