试图构建简单的发送器/接收器,但它们不发送/接收任何东西。
代码发送者:
const peer = new Peer('sender', {key: 'testK', host: '127.0.0.1', port: 9000, debug: 2});
peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
});
var conn = peer.connect('receiver');
console.log(conn);
conn.on('open', function() {
console.log('on open');
conn.send('Hello from sender!');
// Receive messages
conn.on('data', function (data) {
console.log('Received', data);
});
});
代码接收器:
var peer = new Peer('receiver', {key: 'testK', host: '127.0.0.1', port: 9000, debug: 3});
peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
});
//receiving data
peer.on('connection', function(conn) {
console.log('on connection');
conn.on('open', function() {
conn.on('data', function(data) {
console.log(data);
});
});
});
在对等服务器中,我看到消息
Client connected: receiver
Client disconnected: sender
在浏览器控制台中,我看到消息
My peer ID is: sender
My peer ID is: receiver
但没有别的!