kopia lustrzana https://github.com/c9/core
generating uid is only needed on the server
rodzic
181b61e5b6
commit
294d2acb6f
|
@ -22,6 +22,7 @@
|
|||
|
||||
var util = require("./util");
|
||||
var EventEmitter = require("events").EventEmitter;
|
||||
var crypto = require("crypto");
|
||||
|
||||
var ReliableSocket = require("./reliable_socket");
|
||||
var ReconnectSocket = require("./reconnect_socket");
|
||||
|
@ -78,7 +79,7 @@ Server.prototype.getConnection = function(id) {
|
|||
}
|
||||
}
|
||||
|
||||
id = util.uid(10);
|
||||
id = generateUid(10);
|
||||
transport = this.sockets[id] = new ReliableSocket(new ReconnectSocket(), this.socketOptions);
|
||||
transport.on("disconnect", this.disconnect.bind(this, transport));
|
||||
transport.id = id;
|
||||
|
@ -92,4 +93,12 @@ Server.prototype.disconnect = function(transport) {
|
|||
transport.close();
|
||||
this.emit("disconnect", transport);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
function generateUid(length) {
|
||||
return (crypto
|
||||
.randomBytes(length)
|
||||
.toString("base64")
|
||||
.slice(0, length)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -4,32 +4,4 @@ exports.inherits = function(Child, Parent) {
|
|||
Child.prototype = Object.create(Parent.prototype, { constructor: { value: Child }});
|
||||
};
|
||||
|
||||
var isNode = (typeof global !== "undefined" && ({}).toString.call(global) == '[object global]');
|
||||
var isBrowser = (typeof window !== "undefined" && ({}).toString.call(window) == '[object window]');
|
||||
|
||||
if (isNode) {
|
||||
exports.uid = function(length) {
|
||||
// make sure packagers don't try to package "crypto"
|
||||
return (require("cry" + "pto")
|
||||
.randomBytes(length)
|
||||
.toString("base64")
|
||||
.slice(0, length)
|
||||
);
|
||||
};
|
||||
} else if (isBrowser && window.crypto && window.crypto.getRandomValues) {
|
||||
exports.uid = function(length) {
|
||||
var values = new Uint8Array(length);
|
||||
window.crypto.getRandomValues(values);
|
||||
return btoa(String.fromCharCode.apply(null, values)).slice(0, length);
|
||||
};
|
||||
} else {
|
||||
exports.uid = function(length) {
|
||||
var arr = [];
|
||||
for (var i = 0; i < length; i++) {
|
||||
arr[i] = Math.floor(Math.random() * 255);
|
||||
}
|
||||
return btoa(String.fromCharCode.apply(null, arr)).slice(0, length);
|
||||
};
|
||||
}
|
||||
|
||||
});
|
||||
|
|
Ładowanie…
Reference in New Issue