Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify channel operation change #33

Closed
wants to merge 12 commits into from

Conversation

BECATRUE
Copy link
Member

@BECATRUE BECATRUE commented Nov 22, 2024

This is a half part of #17. Before you start to review, please read the issue carefully.

I implemented only the feature in terms of the channel operation change.

The main features are as follows.

  • Use ASGI instead of WSGI (default). For this, I utilized the daphne server, which is the recommended server for Channels.
  • Use Channels to implemented WebSockets.

To test this, one can use the codes below.

Client 1: Listen to the operation change

import requests
from websockets.sync.client import connect

HTTP_BASE_URL = 'http://localhost:8000'
WS_BASE_URL = 'ws://localhost:8000'

with requests.Session() as session:
    data = {'username': 'user1', 'password': 'OOOO'}
    response = session.post(HTTP_BASE_URL + '/user/signin/', json=data)
    print(response.status_code)  # 200

    csrf_token = session.cookies.get('csrftoken')
    headers = {'X-CSRFToken': csrf_token}

    with connect(WS_BASE_URL + '/ws/operation/1/', origin='http://localhost:3000') as websocket:
        while True:
            response = websocket.recv()
            print(response)

Client 2: Update the operation status

import time

import requests

HTTP_BASE_URL = 'http://localhost:8000'

with requests.Session() as session:
    data = {'username': 'user2', 'password': 'OOOO'}
    response = session.post(HTTP_BASE_URL + '/user/signin/', json=data)

    csrf_token = session.cookies.get('csrftoken')
    headers = {'X-CSRFToken': csrf_token}

    response = session.post(HTTP_BASE_URL + '/operation/1/', json={'on': True}, headers=headers)
    time.sleep(1)

    response = session.post(HTTP_BASE_URL + '/operation/1/', json={'on': False}, headers=headers)
    time.sleep(1)

    response = session.post(HTTP_BASE_URL + '/operation/1/', json={'on': True}, headers=headers)
    time.sleep(1)

    response = session.post(HTTP_BASE_URL + '/operation/1/', json={'on': False}, headers=headers)
    time.sleep(1)

    response = session.post(HTTP_BASE_URL + '/operation/1/', json={'on': True}, headers=headers)
    time.sleep(1)

    response = session.post(HTTP_BASE_URL + '/operation/1/', json={'on': False}, headers=headers)
    time.sleep(1)

    response = session.post(HTTP_BASE_URL + '/user/signout/', headers=headers)

@BECATRUE BECATRUE added the enhancement New feature or request label Nov 22, 2024
@BECATRUE BECATRUE added this to the v1.0.0 milestone Nov 22, 2024
@BECATRUE BECATRUE requested a review from kangz12345 November 22, 2024 04:24
@BECATRUE BECATRUE self-assigned this Nov 22, 2024
@BECATRUE
Copy link
Member Author

I will reopen this PR after I complete #34.

@BECATRUE BECATRUE closed this Nov 25, 2024
@BECATRUE BECATRUE added the invalid This doesn't seem right label Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request invalid This doesn't seem right
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant