我正在使用Express和Socket.IO在网络应用中提供实时通知服务。客户端可以连接到服务器,并从服务器接收发射。但是,服务器未从客户端接收发射。我甚至在Express中设置了发布路由,以便在发生服务器端事件时向客户端发送发送,这些工作正常。
客户代码:
<script type="text/javascript">
var socket = io("https://{$_SERVER['HTTP_HOST']}:7076");
socket.on('message', function(data) {
alert(data.message);
});
socket.on('connected', function(data) {
if(data.success) {
socket.emit('join', {
empid: '{$_SESSION['userID']}',
rooms : [
'0{$_SESSION['MasterID']}',
'1{$_SESSION['userDivisionID']}',
'2{$_SESSION['userDistrictID']}',
'3{$_SESSION['userRegionID']}',
'4{$_SESSION['userOfficeID']}'
]
});
}
});
$(document).ready(function(){
var doc = $(document);
doc.on('click', '#join_button', function(){
socket.emit('join', {
empid: '{$_SESSION['userID']}',
rooms : [
'0{$_SESSION['MasterID']}',
'1{$_SESSION['userDivisionID']}',
'2{$_SESSION['userDistrictID']}',
'3{$_SESSION['userRegionID']}',
'4{$_SESSION['userOfficeID']}'
]
});
});
doc.on('click', '#emit_button', function() {
socket.emit('office message', {
office: 3454,
message: 'Hello'
});
});
});
</script>
<div style="padding: 32px;">
<input id="join_button" type="button" class="btn btn-primary" value="Click 2 Join" />
<input id="emit_button" type="button" class="btn btn-primary" value="Click 2 Emit" style="float:right;"/>
</div>
服务器代码:
var app = require('express')(),
https = require('https'),
fs = require('fs'),
routes = require('./mod/routes'),
socketRoutes = require('./mod/socket/routes'),
sockets = require('./mod/socket/sockets'),
config = require('./mod/config'),
socketArray = {},
options = {
key: fs.readFileSync('4096_SSL.key').toString(),
cert: fs.readFileSync('wild.crt').toString(),
ca: fs.readFileSync('gd_bundle.crt').toString()
},
server = https.createServer(options, app);
app.use(require('body-parser').urlencoded({
extended: true
}));
/****************
* Basic Routes *
****************/
app.all('*', routes.setAccessControl);
app.get('/', routes.base);
/*--- Server Side Post Routes ---*/
app.post('/notify/:type', routes.notify);
app.get('/employees/:masterid', routes.employees);
//404 page that doesn't trigger error handling in ajax calls
app.use(routes.fallback);
//start listening on alternate https port
server.listen(7076);
//have socket.io listen to the same https port.
var io = require('socket.io')(server);
/*****************
* Socket Events *
*****************/
io.on('connection', function(socket) {
io.sockets.emit('message', {
message: 'New user Connected'
});
socket.emit('connected', {
success: 1
});
});
io.on('join', function(socket) {
console.log('subscribing');
});
io.on('office message', function(from, data){
console.log(data);
});
服务器正在响应连接事件,客户端正在接收“新用户”消息。但是,如果我单击发出事件的任一按钮,服务器上就不会发生任何事情。我甚至将它绑定在连接事件的回复中,看它是否是一个点击问题。任何想法为什么服务器只是没有响应事件?
答案 0 :(得分:2)
套接字事件必须绑定到套接字本身,而不是IO。尝试这样的事情:
io.on("connection", onSocketConnection);
function onSocketConnection(socket) {
io.sockets.emit('message', { messages: 'New user Connected' });
socket.emit('connected' { success: 1 });
socket.on('join', onClientJoined);
};
function onClientJoined(data) {
console.log('subscribing');
};