Serve/Stop 发球 Socket.IO
Serve/Stop serve Socket.IO
我想知道如何才能 socket.io 只为登录用户提供服务?
目前我只是adding/removing
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
但是当我在会话成功后删除它时,页面没有加载。 using/serving socket.io 是否仅适用于具有护照会话身份验证的用户?
将参数传递给您的视图引擎
例如,我在这里使用车把 -
app.get('/view', ...
res.render('view.html', { authenticated : true } );
在您看来
{{#if authenticated }}
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
{{/if}}
这不会以任何方式保护您的服务器
你也可以这样做 -
<script src="/socket.io/socket.io.js"></script>
<script>
var connect = {{authenticated}} ;
if(connect) {
var socket = io();
}
</script>
真正的答案是使用 SocketIO
框架的所谓 handshake
。它允许您进行一些检查并决定 是否应该允许用户连接到您的服务器。周围的其他答案根本不会自动允许用户连接。但是,如果他只打开一个控制台并针对您的服务器实例化套接字 - 他在线。
看看这个:http://socket.io/docs/server-api/#namespace#use(fn:function):namespace
在每次连接尝试时,您可以运行一个特定的函数来查看是否正常。然后您可以拒绝连接(使用参数调用 next
),或接受连接 - 只需调用 next
.
就是这样:)
但棘手的部分来了 - 如何真正验证用户身份?每个套接字都使用来自客户端的简单 HTTP 请求进行实例化。后来升级为套接字连接。
如果您正在使用某种数据库或会话,您可以使用其中的许多模块。我一直在使用护照,所以一切都会自动发生。以下是有关如何操作的更多信息:https://github.com/jfromaniello/passport.socketio
var io = require("socket.io")(server),
sessionStore = require('awesomeSessionStore'), // find a working session store (have a look at the readme)
passportSocketIo = require("passport.socketio");
io.use(passportSocketIo.authorize({
cookieParser: cookieParser, // the same middleware you registrer in express
key: 'express.sid', // the name of the cookie where express/connect stores its session_id
secret: 'session_secret', // the session_secret to parse the cookie
store: sessionStore, // we NEED to use a sessionstore. no memorystore please
success: onAuthorizeSuccess, // *optional* callback on success - read more below
fail: onAuthorizeFail, // *optional* callback on fail/error - read more below
}));
function onAuthorizeSuccess(data, accept){
console.log('successful connection to socket.io');
// The accept-callback still allows us to decide whether to
// accept the connection or not.
accept(null, true);
// OR
// If you use socket.io@1.X the callback looks different
accept();
}
function onAuthorizeFail(data, message, error, accept){
if(error)
throw new Error(message);
console.log('failed connection to socket.io:', message);
// We use this callback to log all of our failed connections.
accept(null, false);
// OR
// If you use socket.io@1.X the callback looks different
// If you don't want to accept the connection
if(error)
accept(new Error(message));
// this error will be sent to the user as a special error-package
// see: http://socket.io/docs/client-api/#socket > error-object
}
我想知道如何才能 socket.io 只为登录用户提供服务?
目前我只是adding/removing
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
但是当我在会话成功后删除它时,页面没有加载。 using/serving socket.io 是否仅适用于具有护照会话身份验证的用户?
将参数传递给您的视图引擎
例如,我在这里使用车把 -
app.get('/view', ...
res.render('view.html', { authenticated : true } );
在您看来
{{#if authenticated }}
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
{{/if}}
这不会以任何方式保护您的服务器
你也可以这样做 -
<script src="/socket.io/socket.io.js"></script>
<script>
var connect = {{authenticated}} ;
if(connect) {
var socket = io();
}
</script>
真正的答案是使用 SocketIO
框架的所谓 handshake
。它允许您进行一些检查并决定 是否应该允许用户连接到您的服务器。周围的其他答案根本不会自动允许用户连接。但是,如果他只打开一个控制台并针对您的服务器实例化套接字 - 他在线。
看看这个:http://socket.io/docs/server-api/#namespace#use(fn:function):namespace
在每次连接尝试时,您可以运行一个特定的函数来查看是否正常。然后您可以拒绝连接(使用参数调用 next
),或接受连接 - 只需调用 next
.
就是这样:)
但棘手的部分来了 - 如何真正验证用户身份?每个套接字都使用来自客户端的简单 HTTP 请求进行实例化。后来升级为套接字连接。
如果您正在使用某种数据库或会话,您可以使用其中的许多模块。我一直在使用护照,所以一切都会自动发生。以下是有关如何操作的更多信息:https://github.com/jfromaniello/passport.socketio
var io = require("socket.io")(server),
sessionStore = require('awesomeSessionStore'), // find a working session store (have a look at the readme)
passportSocketIo = require("passport.socketio");
io.use(passportSocketIo.authorize({
cookieParser: cookieParser, // the same middleware you registrer in express
key: 'express.sid', // the name of the cookie where express/connect stores its session_id
secret: 'session_secret', // the session_secret to parse the cookie
store: sessionStore, // we NEED to use a sessionstore. no memorystore please
success: onAuthorizeSuccess, // *optional* callback on success - read more below
fail: onAuthorizeFail, // *optional* callback on fail/error - read more below
}));
function onAuthorizeSuccess(data, accept){
console.log('successful connection to socket.io');
// The accept-callback still allows us to decide whether to
// accept the connection or not.
accept(null, true);
// OR
// If you use socket.io@1.X the callback looks different
accept();
}
function onAuthorizeFail(data, message, error, accept){
if(error)
throw new Error(message);
console.log('failed connection to socket.io:', message);
// We use this callback to log all of our failed connections.
accept(null, false);
// OR
// If you use socket.io@1.X the callback looks different
// If you don't want to accept the connection
if(error)
accept(new Error(message));
// this error will be sent to the user as a special error-package
// see: http://socket.io/docs/client-api/#socket > error-object
}