te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Django channels using secured WebSocket connection - WSS: - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Django channels using secured WebSocket connection - WSS: - Stack Overflow

programmeradmin3浏览0评论

When I tried to run the Django application using sslserver as shown below,

python manage.py runsslserver

Errors:

Traceback:

Validating models...

System check identified no issues (0 silenced).
November 08, 2019 - 11:17:26
Django version 2.0.7, using settings 'dashboard_channels.settings'
Starting development server at https://127.0.0.1:8000/
Using SSL certificate: \lib\site-packages\sslserver\certs\development.crt
Using SSL key: \lib\site-packages\sslserver\certs\development.key
Quit the server with CTRL-BREAK.
[08/Nov/2019 11:18:33] "GET / HTTP/1.1" 200 1299
[08/Nov/2019 11:18:34] "GET / HTTP/1.1" 200 1299
[08/Nov/2019 11:18:35] "GET /static/js/jquery.js HTTP/1.1" 200 270575
Not Found: /ws/home
[08/Nov/2019 11:18:36] "GET /ws/home HTTP/1.1" 404 2134

Browser Console:

(index):31 WebSocket connection to 'wss://127.0.0.1:8000/ws/home' failed: Error during WebSocket handshake: Unexpected response code: 404
(index):41 error Event
(index):44 close CloseEvent

Code:

Javascript:

 var loc = window.location;
 var wsStart = 'ws://';
 if (loc.protocol == 'https:') {
     wsStart = 'wss://'
 }
 var endpoint = wsStart + loc.host + '/ws/home';

 var socket = new WebSocket(endpoint);

It's working fine with python manage.py runserver mand, means for http it's working but not with https.

How to resolve this issue? (How to debug to sort out this issue?)

Is there any other way to deploy WebSockets on https portal?

Still facing this issue. Can anyone please help?

Anyhow this is for testing purposes, finally, I need to deploy it on Apache2.4 in the windows server machine. Where I have already set up for https but not for web sockets.

When I tried to run the Django application using sslserver as shown below,

python manage.py runsslserver

Errors:

Traceback:

Validating models...

System check identified no issues (0 silenced).
November 08, 2019 - 11:17:26
Django version 2.0.7, using settings 'dashboard_channels.settings'
Starting development server at https://127.0.0.1:8000/
Using SSL certificate: \lib\site-packages\sslserver\certs\development.crt
Using SSL key: \lib\site-packages\sslserver\certs\development.key
Quit the server with CTRL-BREAK.
[08/Nov/2019 11:18:33] "GET / HTTP/1.1" 200 1299
[08/Nov/2019 11:18:34] "GET / HTTP/1.1" 200 1299
[08/Nov/2019 11:18:35] "GET /static/js/jquery.js HTTP/1.1" 200 270575
Not Found: /ws/home
[08/Nov/2019 11:18:36] "GET /ws/home HTTP/1.1" 404 2134

Browser Console:

(index):31 WebSocket connection to 'wss://127.0.0.1:8000/ws/home' failed: Error during WebSocket handshake: Unexpected response code: 404
(index):41 error Event
(index):44 close CloseEvent

Code:

Javascript:

 var loc = window.location;
 var wsStart = 'ws://';
 if (loc.protocol == 'https:') {
     wsStart = 'wss://'
 }
 var endpoint = wsStart + loc.host + '/ws/home';

 var socket = new WebSocket(endpoint);

It's working fine with python manage.py runserver mand, means for http it's working but not with https.

How to resolve this issue? (How to debug to sort out this issue?)

Is there any other way to deploy WebSockets on https portal?

Still facing this issue. Can anyone please help?

Anyhow this is for testing purposes, finally, I need to deploy it on Apache2.4 in the windows server machine. Where I have already set up for https but not for web sockets.

Share Improve this question edited Jul 17, 2024 at 11:02 Sam Mason 16.2k1 gold badge45 silver badges70 bronze badges asked Nov 8, 2019 at 6:18 shaik moeedshaik moeed 5,7852 gold badges23 silver badges59 bronze badges 5
  • 1 It says Not Found: /ws/home. Does it say the same for http? – ege Commented Nov 12, 2019 at 8:45
  • 2 No, it's working properly. There is no issue with http. – shaik moeed Commented Nov 12, 2019 at 8:46
  • 1 @shaikmoeed Please check if you configured CHANNEL_LAYERS in settings.py – wowkin2 Commented Dec 5, 2019 at 10:22
  • 1 @wowkin2 CHANNEL_LAYERS was configured and it's working totally fine with http. How to run on https? – shaik moeed Commented Dec 5, 2019 at 10:25
  • 2 @shaikmoeed have you tried to use Daphne for that? If not - really remend to do that: github./django/daphne – wowkin2 Commented Dec 5, 2019 at 13:15
Add a ment  | 

1 Answer 1

Reset to default 13

I found the answer, the runserver mand correctly detects asgi.py file and run the Django channels application on WebSockets using daphne. Somehow runsslserver is not doing the same job, It's running the wsgi.py file instead of asgi.py file.

After reading different approaches, I get to know that we can handle HTTPS request using our normal development server (i.e., using wsgi.py file) and wss request by using Daphne(i.e., using asgi.py file).

Daphne was an officially designed server to handle django-channels(built on the top of the twisted module).

So, finally, we need to run two servers to handle https and wss separately.

# In mand prompt 1 (For production, use Apache or Nginx to serve HTTP requests)
python manage.py runsslserver 0.0.0.0:8000

# In mand prompt 2 (This works for production as well).
daphne -e ssl:8001:privateKey=cert\\private.pem:certKey=cert\\public.pem real_time_table.asgi:application

We should use the same SSL certificates used by runsslserver for testing.

Finally, in JavaScript:

var loc = window.location;
var wsStart = 'ws://';
if (loc.protocol == 'https:') {
     wsStart = 'wss://'
}
// var endpoint = wsStart + 'your_ip_address:port_given_to_daphne_server' + '/ws/home';
// For above mand, it look like this
var endpoint = wsStart + 'xxx.xx.xx.xxx:8001' + '/ws/home';
// Note the websocket port is 8001
var socket = new WebSocket(endpoint);

I hope, this will save someone's time.

发布评论

评论列表(0)

  1. 暂无评论