gpt4 book ai didi

node.js - Socket.io 聊天应用程序在我发送消息时刷新

转载 作者:搜寻专家 更新时间:2023-10-31 23:54:46 25 4
gpt4 key购买 nike

我正在尝试使用 Node.js、socket.io 和 express 制作一个简单的聊天应用程序。但是,如果我单击 main.jade 文件中的发送按钮,页面会刷新,并且不会出现任何消​​息。我在 Firebug 中也遇到了这个错误:

The connection to ws://127.0.0.1:3000/socket.io/?EIO=2&transport=websocket&sid=d_hNMPdXHed-j7LrAAAH was interrupted while the page was loading.

主.jade

doctype html
html
head
meta(charset = "UTF-8")

script(src="/socket.io/socket.io.js")
script(src="http://code.jquery.com/jquery-1.11.1.js")
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));
});

body
ul#messages
form(action = "")
input#m(type = "text")
button Send

应用程序.js

var express = require('express')
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var mongo = require('mongodb');
var mongodb = require("mongodb");
var monk = require('monk');
var db = monk('localhost:27017/pesterchum');


var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(function(req,res,next){
req.db = db;
next();
});

app.use('/', routes);
app.use('/users', users);

/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

/// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;

www

#!/usr/bin/env node
var debug = require('debug')('app');
var app = require('../app');

app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});

var io = require('socket.io').listen(server);

io.sockets.on('connection', function(socket){
socket.on('chat message', function(msg){
io.sockets.emit('chat message', msg);
});
});

索引.js

var express = require('express');
var router = express.Router();

/* GET Main page. */
router.get('/main', function(req, res) {
res.render('main', { title: 'Main'});
});

最佳答案

似乎页面正在重新加载,因为您忘记用 $(document).ready(function() { ... }); 包装您的客户端 js 代码。

关于node.js - Socket.io 聊天应用程序在我发送消息时刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24597528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com