我构建了使用 socket.io 和 expressjs 的小型应用程序
服务器端
var express = require('express'),
sio = require('socket.io');
var app = express.createServer();
app.configure('development', function(){
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.static(__dirname + '/'));
app.set('views', __dirname + '/views');
app.use(express.errorHandler({dumpExceptions: true, showStack: true}));
app.use(app.router);
});
app.listen(4000);
var io = sio.listen(app);
app.get('/', function (req, res) {
res.redirect('index.html');
});
io.sockets.on('connection', function (socket) {
app.post('/mysegment',function(req,res){
var userData = req.body;
socket.emit('sendUser', userData);
res.send("yes I got that segment");
});
socket.on('getUser',function(msg){
console.log("Yes Socket work Properly "+msg);
});
});
和 index.html
var socket = io.connect();
socket.on('connect', function () {
console.log("client connection done.....");
});
socket.on('sendUser', function (data) {
alert("On send user");
socket.emit('getUser',"Hello");
});
这个演示工作完美但是当我刷新页面并将发布请求发送到“/mysegment”时,我的时间套接字无法正常工作。我没有在我的控制台上收到消息“是的套接字工作正常(和我的消息)”但我得到了回应“是的,我得到了那个段”任何建议请...