我正在MEAN堆栈上运行,并且在连接到socket.io
后,我的服务器控制台在grunt正在服务的时候发出了类似这样的GET请求:
GET /socket.io/?EIO=3&transport=polling&t=1438905856120-1645 200 49ms
GET /socket.io/?EIO=3&transport=polling&t=1438905856204-3489 200 41ms
GET /socket.io/?EIO=3&transport=polling&t=1438905856408-1648 200 16ms
等
什么是交通投票?我要做的就是按照Socket.io的说明向服务器控制台发出聊天消息。下面我将包含相关的代码片段:
app.js(服务器端)
socketio.sockets.on('connection', require('./config/socketio'));
Socket.io配置(客户端)
socketio.on('connection', function (socket) {
console.log("hello");
// socket.address = socket.handshake.address !== null ?
// socket.handshake.address.address + ':' + socket.handshake.address.port :
// process.env.DOMAIN;
socket.connectedAt = new Date();
// Call onDisconnect.
socket.on('disconnect', function () {
console.log("goodbye");
onDisconnect(socket);
console.info('[%s] DISCONNECTED', socket.address);
});
// Call onConnect.
onConnect(socket);
console.info('[%s] CONNECTED', socket.address);
});
Controller.js
var socket = io();
$scope.sendMessage = function(){
socket.emit('chat message', $('.new-message').val());
$('.new-message').val('');
return false;
};
HTML
<form class="message-form" ng-submit="sendMessage()">
<input style="width:100%" class="new-message" placeholder="Type your message here..." ng-model="newMessage" ng-trim="false">
<button type="submit">Send</button>
</form>
修改的
app.js (服务器端)
var app = express();
var server = require('http').createServer(app);
var socketio = require('socket.io').listen(server, {
serveClient: (config.env === 'production') ? false : true,
path: '/socket.io-client'
});