我是nodejs的新手,表达和尝试 socket.io 只是一个简单的聊天应用程序。它非常简单的代码一切都很好,但节点控制台上没有事件触发日志。连接事件代码不起作用。
应用.js
var express = require('express'),
app = require('express')(),
http = require('http').Server(app),
io = require('socket.io')(http),
start = require('./routes/index.js');
app.use('/', start);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
});
module.exports = app;
布局.翡翠
doctype html
html(lang="en")
head
script(type="text/javascript" src="https://cdn.socket.io/socket.io-1.4.3.js")
script.
var socket = io();
body
block content
尝试在 http 而不是 app 上收听。喜欢这个:
var server = http.listen(3000, function () {
...