Skip to content

Instantly share code, notes, and snippets.

@2n2n
Created May 26, 2019 02:29
Show Gist options
  • Save 2n2n/cab50805943f9b1bf75e7c7d6aa6c0d6 to your computer and use it in GitHub Desktop.
Save 2n2n/cab50805943f9b1bf75e7c7d6aa6c0d6 to your computer and use it in GitHub Desktop.
python3 flask-restful issue
from flask import Flask
from flask_restful import Resource, Api
from flask_cors import CORS
app = Flask(__name__)
api = Api(app)
CORS(app)
def get(self):
args = request.args
try:
channel_name = args['channel_name']
socket_id = args['socket_id']
username = args['username']
custom = {
'user_id': '%d' % random.randint(100, 999),
'user_info': { 'username': username}
}
auth = self.pusher.authenticate(
channel=channel_name,
socket_id=socket_id,
custom_data=custom
)
return json.dumps(auth)
except ValueError as error:
print(error)
# add auth
api.add_resource(Auth, '/pusher/auth')
import Pusher from 'pusher-js';
export default class pusherService {
public static connect(username: string) {
Pusher.logToConsole = true;
const key: string = '4de2f58b95c9bda94f4f';
const cluster: string = 'ap1';
const forceTLS: boolean = true;
let _pusher = new Pusher(key, {
authTransport: 'jsonp',
authEndpoint: 'https://2ec71c69.ngrok.io/pusher/auth',
cluster: cluster,
encrypted: false,
auth: {
params: { username: username }
},
forceTLS: forceTLS
});
_pusher.connection.bind('state_change', (states: any) => console.log('pusher is :', states.current))
let channel = _pusher.subscribe('private-10-lobby');
channel.bind('pusher:subscription_success', () => {
console.log('subscription succeeded');
})
channel.bind('pusher:subscription_error', (status: any) => {
console.log('some error happened', status);
})
// listen to event on debug when subscription succeeded
channel.bind('my-event', (msg: any) => {
console.log(msg, 'wahehe');
})
return _pusher;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment