Closed richardanaya closed 11 years ago
Does this only occur with a message of precisely 1000 characters? Or does it occur at others times do? Are the characters possibly UTF or just ascii?
It happens with values other than 1000 as well, here is a complete example of it failing with 500 (sorry about the mess)
<html>
<head>
<script src="http://code.jquery.com/jquery-2.0.1.min.js"></script>
<script src="http://cdn.peerjs.com/0/peer.min.js"></script>
</head>
<body>
<button id="server">Server</button><button id="client">Client</button>
</body>
<script>
var Util = function(){};
Util.int32ToString = function(num){
var byte0 = num >> 24;
var byte1 = (num << 8) >> 24;
var byte2 = (num << 16) >> 24;
var byte3 = (num << 24) >> 24;
if(byte0<0){byte0 = 256+byte0;}
if(byte1<0){byte1 = 256+byte1;}
if(byte2<0){byte2 = 256+byte2;}
if(byte3<0){byte3 = 256+byte3;}
console.log(byte0+" "+byte1+" "+byte2+" "+byte3)
return String.fromCharCode(byte0)+String.fromCharCode(byte1)+String.fromCharCode(byte2)+String.fromCharCode(byte3);
}
Util.stringToInt32 = function(str){
var byte0 = str.charCodeAt(0);
var byte1 = str.charCodeAt(1);
var byte2 = str.charCodeAt(2);
var byte3 = str.charCodeAt(3);
return byte0 << 24 | byte1 << 16 | byte2 << 8 | byte3;
};
var lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu est at mauris auctor tincidunt vel eget lorem. Duis et justo sed leo dignissim consequat. Donec elementum ipsum vitae mauris interdum vitae faucibus orci facilisis. Donec eu tortor metus, id mollis elit. Proin vulputate ligula sit amet quam mollis et venenatis eros lobortis. Phasellus varius orci accumsan est mollis ut sollicitudin est egestas. In tempor leo et nulla rhoncus at viverra neque accumsan. Nulla ac odio libero. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod diam et mauris blandit pellentesque. Proin aliquam, elit sed dapibus gravida, tellus justo semper tellus, id placerat elit metus in leo. Nullam lacinia placerat tincidunt. In sit amet pellentesque lectus.<br><br>Proin urna eros, dictum in blandit vitae, sodales a elit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla vitae neque a magna fringilla accumsan. Mauris sed augue sed elit scelerisque placerat. Vestibulum dolor tortor, iaculis et rhoncus rhoncus, sagittis eu diam. Nunc eget leo neque. Donec nunc libero, ultricies et eleifend id, pharetra id quam. Duis nisi ante, tempus vitae lacinia egestas, posuere sed nisi. Aliquam libero dolor, volutpat eget euismod eu, vehicula quis lectus. Integer suscipit dapibus diam ut tristique. Integer et dui elit. Vivamus dui magna, commodo nec accumsan non, consequat vitae est. Vestibulum vestibulum nunc ut est vulputate scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In facilisis porttitor semper. Duis sed dignissim ipsum.<br><br>Donec euismod orci quis leo feugiat tincidunt. Suspendisse potenti. Fusce enim arcu, tempus ac pretium condimentum, tincidunt nec magna. Quisque vitae nisl et magna vestibulum pharetra nec et erat. Praesent vel augue ut nibh elementum congue. Pellentesque accumsan, libero ac lobortis fermentum, ligula diam dapibus nisi, sit amet tincidunt mauris tellus in odio. Mauris et pharetra lectus. Suspendisse sed erat id risus dignissim fermentum et id velit. Vestibulum fermentum mi sit amet ante aliquam vel adipiscing justo pulvinar. Donec hendrerit rutrum posuere. Morbi adipiscing dignissim mauris ut pellentesque.<br><br>Quisque egestas lacinia orci, sit amet scelerisque sapien ultrices ut. Sed id nibh non dolor consequat accumsan sed ultrices ante. Phasellus id est diam, et condimentum ligula. Integer vel rutrum mi. Pellentesque sollicitudin, dui eu tempor varius, augue mauris venenatis dui, sit amet tincidunt sapien tortor et leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur ut ante non lorem tincidunt vehicula a aliquam ligula.<br><br>Quisque lacinia tristique eros, eget adipiscing eros pretium pulvinar. Phasellus at odio id purus consequat commodo. Phasellus lorem dolor, scelerisque eu mattis vitae, tincidunt non nulla. Morbi felis tortor, lacinia eget rhoncus eget, fringilla in massa. Vivamus porta lacinia aliquet. Donec pharetra lectus nec risus pulvinar et adipiscing massa interdum. Phasellus dui lorem, euismod id faucibus sed, elementum quis ipsum. Sed lacinia diam at justo tempor sed feugiat sem porttitor. Integer mattis massa dictum nulla laoreet vitae dictum leo malesuada. Vivamus vulputate fringilla libero vel semper. Nulla egestas fermentum convallis. Aliquam odio sapien, auctor sit amet molestie dictum, sagittis ut mi.';
var startClient = function(){
var chunk = lorem.substr(0,500-13);
var header = "";
header += Util.int32ToString(0);
header += Util.int32ToString(0);
header += Util.int32ToString(500-13);
var c = 1;
header += String.fromCharCode(c);
var testString = header+chunk
var peer = new Peer({key: '<your key>'});
var conn = peer.connect('abc');
conn.on('open', function() {
console.log(testString.length);
conn.send(testString);
});
};
var startServer = function(){
var peer = new Peer('abc', {key: '<your key>'});
peer.on('connection', function(conn){
conn.on('data', function(data) {
console.log(data.length);
});
});
};
$('#server').click(startServer);
$('#client').click(startClient);
</script>
</html>
Looking into it now
Thanks, also, fixed the code slightly, was referencing a non-existent variable that should have been zero.
Yep the issue is indeed because this line: header += Util.int32ToString(500-13);
introduces a UTF-8 character in the message as a string.
There are two workarounds you can choose from:
binary-utf8
instead of the default binary
serialization, so change var conn = peer.connect('abc');
to var conn = peer.connect('abc', {serialization: 'binary-utf8'});
UTF8 binary is not enabled by default because it takes a performance hit.
Hmm, you sure about that? The whole point of Util.int32ToString(500-13) is to turn a number into four separate ascii string characters. Maybe i'm missing something. In the case of 487 it should make:
0 0 1 231
Is String.fromCharCode not compatible with ascii?
Oh ^o^ is ascii only 7 bits?
Version 28.0.1500.20 beta on Mac
I sent a message of length 1000 characters, and received a string of length 999.
This consistently occurs.