Coder Social home page Coder Social logo

Comments (11)

ericz avatar ericz commented on May 22, 2024

Hey Majid,

I can't replicate this, can you post the specific code you're running?

On Tue, May 28, 2013 at 11:33 PM, Majid Fouladpour <[email protected]

wrote:

Trying the code in Getting started guide, I get this error in Google
Chrome console:

Failed to load resource: the server responded with a status of 401
(Unauthorized)

Opening the link in a new tab, this is the json sent with header 401:

{"code":"MethodNotAllowedError","message":"GET is not allowed"}

Am I missing a required step?


Reply to this email directly or view it on GitHubhttps://github.com//issues/52
.

510-691-3951
http://ericzhang.com

from peerjs.

igrigorik avatar igrigorik commented on May 22, 2024

Here's an example:

<!DOCTYPE html>
<html>
<head>
    <script src="http://cdn.peerjs.com/0/peer.min.js"></script>
</head>

<body>
</body>

<script>
  var local = new Peer('localID', {key: '...'});
  local.on('connection', function(conn) {
    console.log("LOCAL connection open", conn)      
    conn.on('data', function(data){        
      console.log("LOCAL got message", data);
    });
  });
</script>

<script>
  var remote = new Peer('remoteID', {key: '...', debug:true});
  var conn = remote.connect('localID');
  console.log("REMOTE connecting to LOCAL", conn)

  conn.on('open', function(){
    console.log("connect to REMOTE opened", conn);
    conn.send('hi!');
  }); 
</script>

</html>

Above works fine in FF, but fails in Chrome: 401 unathorized. Haven't tracked it down yet.. but I'm 99% sure it has something to do with CORS and Chrome not being happy about it. Netlog:

t=1373081418783 [st=  6]     +HTTP_TRANSACTION_SEND_REQUEST  [dt=0]
t=1373081418783 [st=  6]        HTTP_TRANSACTION_SEND_REQUEST_HEADERS
                                --> POST /.../remoteID/.../candidate HTTP/1.1
                                    Host: 0.peerjs.com:9000
                                    Connection: keep-alive
                                    Content-Length: 196
                                    Cache-Control: no-cache
                                    Pragma: no-cache
                                    Origin: http://localhost:8000
                                    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1558.0 Safari/537.36
                                    Content-Type: application/json
                                    Accept: */*
                                    Referer: http://localhost:8000/peerjs.html
                                    Accept-Encoding: gzip,deflate,sdch
                                    Accept-Language: en-US,en;q=0.8
t=1373081418783 [st=  6]        HTTP_TRANSACTION_SEND_REQUEST_BODY
                                --> did_merge = true
                                --> is_chunked = false
                                --> length = 196
t=1373081418783 [st=  6]     -HTTP_TRANSACTION_SEND_REQUEST
t=1373081418783 [st=  6]     +HTTP_TRANSACTION_READ_HEADERS  [dt=100]
t=1373081418783 [st=  6]        HTTP_STREAM_PARSER_READ_HEADERS  [dt=100]
t=1373081418883 [st=106]        HTTP_TRANSACTION_READ_RESPONSE_HEADERS
                                --> HTTP/1.1 401 Unauthorized
                                    Access-Control-Allow-Origin: *
                                    Access-Control-Allow-Methods: GET,PUT,POST,DELETE
                                    Access-Control-Allow-Headers: Content-Type
                                    Date: Sat, 06 Jul 2013 03:30:18 GMT
                                    Connection: keep-alive
                                    Transfer-Encoding: chunked
t=1373081418883 [st=106]     -HTTP_TRANSACTION_READ_HEADERS
t=1373081418883 [st=106]   -URL_REQUEST_START_JOB
t=1373081418884 [st=107]    HTTP_TRANSACTION_READ_BODY  [dt=0]

from peerjs.

michelle avatar michelle commented on May 22, 2024

Hrm. Are you sure the API key you're using is valid?

I tried this code with our demo API key and got the following logs:

PeerJS:  Data channel connection success peer.min.js:1
connect to REMOTE opened 
l {_events: Object, open: true, label: "peerjs", metadata: undefined, serialization: "binary"…}
 test.html:27
PeerJS:  Data channel connection success peer.min.js:1
LOCAL got message hi! 

from peerjs.

michelle avatar michelle commented on May 22, 2024

Ah, I think it's an issue with the connection to the server. This may be the same issue as #55.

I will investigate, but the best way to get it to work for now is by not calling connect until the connection to the server opens.

See fixed code below.

<!DOCTYPE html>
<html>
<head>
    <script src="http://cdn.peerjs.com/0/peer.min.js"></script>
</head>

<body>
</body>

<script>
  var local = new Peer('localID', {key: '...'});
  local.on('connection', function(conn) {
    console.log("LOCAL connection open", conn)      
    conn.on('data', function(data){        
      console.log("LOCAL got message", data);
    });
  });

  var remote = new Peer('remoteID', {key: '...', debug:true});

  // ADDED THIS:
  remote.on('open', function() {
    var conn = remote.connect('localID');
    console.log("REMOTE connecting to LOCAL", conn)

    conn.on('open', function(){
      console.log("connect to REMOTE opened", conn);
      conn.send('hi!');
    });
  }); 
</script>

</html>

from peerjs.

michelle avatar michelle commented on May 22, 2024

Won't close for now though, since I'm not quite sure your 401 issue is the same as @majid4466's

Also, very weird: I can't get it to repro again for that original block of code.

from peerjs.

igrigorik avatar igrigorik commented on May 22, 2024

Nevermind my "99%" comment earlier - it's not Chrome. It may be a race with WS vs XHR here...

from peerjs.

michelle avatar michelle commented on May 22, 2024

Ah, that could be the case. When WS connects the remaining XHR requests are 401ed.

from peerjs.

igrigorik avatar igrigorik commented on May 22, 2024

Right, that's the bug.. WS overrides the hash with proper IP, whereas XHR's are setting "undefined" for IP. Just sent a pull which should (hopefully) fix this.

from peerjs.

michelle avatar michelle commented on May 22, 2024

Should be fixed. Feel free to reopen if this happens again.

from peerjs.

minusplusminus avatar minusplusminus commented on May 22, 2024

I'm currently testing the connection with the peer.js server and simulating bad connections, reconnecting multiple times per minute to see what happens. Sometimes I get the same error.

from peerjs.

shah-smit avatar shah-smit commented on May 22, 2024

I did get this error earlier this morning, however, the issue was to do with http(s) request.

from peerjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.