S
S
sportik1742022-03-01 13:12:09
Django
sportik174, 2022-03-01 13:12:09

How to setup socket.io client for django channels?

Here is the code on the client:

const lat = 132;
        const lng = 132;
        const server = 'wss://example.ru';
        const login = '123';
        const pass = 'qwerty123';
        const socket = io.connect(
            server, 
            { 
                transports: ["websocket"],
                path: "/ws/test/"+login+"/"+pass+"/"
            }
        );


There is this code on the server on the Django channels file consumes.py :
class TestConsumer(WebsocketConsumer):
    """ Соединие с приложением """
    def connect(self):
        user_login = self.scope['url_route']['kwargs']['user_id']
        user_password = self.scope['url_route']['kwargs']['password']
        print(user_login)
        print(user_password)
        print(self.scope)
        if user_password == 'qwerty123':
            print('Пароль верен')
            self.accept()
            self.send(text_data="2000")
        else:
            print('Пароль неверен')
            self.close()

    def receive(self,text_data=None, bytes_data=None):
        print(text_data)
        print(bytes_data)
        
    def disconnect(self, close_code):
        # print('disconnect')
        pass


When trying to connect from the client, it connects and disconnects almost immediately

. However, this code on the client works fine with the server:
var url = 'wss://example.ru/ws/test/';
            var user_login = '123';
            var user_password = 'qwerty123';

            var chatSocket = new WebSocket(
                url+user_login+'/'+user_password+'/'
            );

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question