0
創建index.js(服務器)在openshift Nodejs Socket.io響應200好,但console.log值不可見。我的套接字正在工作?
var express = require('express');
var app = express();
///creating server
var server = require('http').createServer(app);
var io = require('socket.io').listen(server, { origins:'http://nodejs-atnodejs.rhcloud.com:8000' });
下面
被剩餘代碼 路由到index.html頁面
app.get('/', function (req, res) {
console.log('in socket---' + res);
res.sendfile('index.html');
});
///socket connection
io.sockets.on('connection', function (socket) {
socket.on('chatmessage', function (msg) {
io.emit('chatmessage', msg);
console.log('in socket---' + data);
});
});
/// Listen to Openshift port
server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);
創建的index.html(客戶端)
src="http://nodejs-atnodejs.rhcloud.com:8000/socket.io/socket.io.js
var socket = io.connect('http://nodejs-atnodejs.rhcloud.com:8000');
console.log('this is index page');
socket.on('chatmessage', function (data) {
console.log('chatmessage---' + data);
socket.emit('chatmessage', { my: 'data' });
});
Problem is not getting "console.log('chatmessage---' + data);" which is inside the socket..
and keep on getting xhr-polling../t=xxxxx responses..
is my socket working properly?
謝謝,但它不工作。這裏是我的完整代碼https://github.com/varund29/openshift/blob/master/index.html和https://github.com/varund29/openshift/blob/ master/index.js請讓我知道如果有任何錯誤的。 –
無論哪種方式,您的代碼都需要爲您的事件偵聽器發出聊天消息事件來接收它。你必須「開啓」等待事件發出的事件。 – ckross01
我參考了上面的http://socket.io/get-started/chat/ –