简体   繁体   中英

POST pusher/auth 404 (Not Found)

I'm trying to establish a private channel using Pusher on a local node.js server. For some reason, I can't get my auth endpoint to play nice and I keep getting a 404 error.

At first I thought it was an issue with how I was defining my endpoint in relation to the location of the local server, but I don't think that's a problem. More likely, my noobiness with server-client-api communication means I'm missing some big piece.

I've looked through the authentication docs on Pusher and literally every SO thread I could find, but to no avail.

I've got Node installed and the server running, and Pusher recognizes that a connection is made, I'm just failing at the authentication.

Any help at all would be mightily appreciated.

Here's the client-side JS that is called when a button is clicked over at index.html:

In client.js:

    function startGame(){
    var nameinput = prompt("Give your game a name","My Game");
    if (nameinput !== null) {
        var initialsinput = prompt("What are your initials?", "MG");
        if (initialsinput !== null) {
            var pusher = new Pusher(key);
            Pusher.channel_auth_endpoint = 'http://localhost:8080/pusher/auth.js';
            var channel = pusher.subscribe("private-"+gamename);
            joined = 'client-opponent_joined'+gamename;
            channel.bind('client-opponent_joined'+gamename, function(data) {
                OnLikeDonkeyKong(data.nameinput,data.initialsinput);
            });
        }
        else {alert("I need your initials.");}
    }
    else {alert ("I need a game name.");}
}

Then, over in /pusher/auth.js:

var express = require( 'express' );
var Pusher = require( 'pusher' );

    var app = express( express.logger() );
    app.use( express.bodyParser() );
    
    var pusher = new Pusher( { appId: 'xxx', key: 'xxx', secret: 'xxx' } );
    
    app.post( '/pusher/auth.js', function( req, res ) {
      var socketId = req.body.socket_id;
      var channel = req.body.channel_name;
      var auth = pusher.authenticate( socketId, channel );
      res.send( auth );
    } );
    
    var port = process.env.PORT || 8080;
    app.listen( port );

Finally, here's the error I'm getting:

POST http://localhost:8080/pusher/auth.js 404 (Not Found)

http://localhost:8080/pusher/auth.js

This url is not exist on server. Check the location of auth.js again.

From Pusher document ( link )

authEndpoint (String)

Endpoint on your server that will return the authentication signature needed for private and presence channels. Defaults to '/pusher/auth'.

So you need to create your authentication endpoint on your server and provide a link to it while setup Pusher instance to authenticate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM