Comments (7)
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?
from peerjs.
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>
from peerjs.
Looking into it now
from peerjs.
Thanks, also, fixed the code slightly, was referencing a non-existent variable that should have been zero.
from peerjs.
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:
- Use the
binary-utf8
instead of the defaultbinary
serialization, so changevar conn = peer.connect('abc');
tovar conn = peer.connect('abc', {serialization: 'binary-utf8'});
- Send your message as binary data (in a Blob or ArrayBuffer) instead of a string, and then change it back to string on the receiving end.
UTF8 binary is not enabled by default because it takes a performance hit.
from peerjs.
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?
from peerjs.
Oh ^o^ is ascii only 7 bits?
from peerjs.
Related Issues (20)
- ERROR PeerJS: Error: Could not connect to peer
- Peerjs reconnecting not working fine sometime one of the device is not connected
- Having problem with Blob in v 1.5.1 peerjs.min.js HOT 6
- Insufficient postMessage Validation HOT 2
- ReferenceError: navigator is not defined when using nestjs HOT 4
- Call answer for a stream coming from screen share on peer which does not have a webcam shows nothing HOT 2
- New maintainer(s) wanted!
- connection problem HOT 3
- Play() was interrupted by new load request
- Peer TURN servers seems to be down HOT 2
- peer.connect not open on some users
- Remote video stream shows black screen. HOT 2
- Works only for same network HOT 5
- Remote video transmission not working again
- 'close' event on DataConnection is not emitted on Firefox HOT 1
- New feature: add 'allow_override_connect' startup option
- [electron] Can't establish connection between two local clients HOT 2
- PeerJS example for p2p http
- Slow initial connection speed HOT 3
- Working on LAN Perfectly but also working in WAN, but in WAN if there is network latency the connection was interrupting. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from peerjs.