Python으로 WebRTC 시그널링 서버 만들기 #webrtckr
파이썬의 어드벤트 캘린더 담당 일이지만 WebRTC의 모쿠모쿠 모임에 왔습니다.
어드벤트 캘린더를 채우자! WebRTC 모쿠모쿠 모임
모처럼이므로 Python으로 WebRTC의 시그널링 서버를 만들어 보겠습니다.
현재라고 시그널링 서버라고 할까 시그널링 해 보았다고 느끼므로 업데이트하고 싶다 [WIP]
↑했습니다.
평소에는 파이썬을 사용하지 않으므로 뉘앙스가 다르다는 것이 있으면 알려주세요.
지금까지 한 일
시그널링 만하고 있습니다 ()
조사
Python은 평소 만지지 않기 때문에 조사에서 시작입니다.
등이 발견되었습니다.
simple-websocket-server가 확실히 간단하고 이해하기 쉽기 때문에 이것을 사용해 보겠습니다.
어쩌면 마지막 링크가 공식 느낌의 사람이지만 이번에는 쓰기 속도 중시로 ...... 웃음
simple-websocket-server를 만져보세요
서버측
우선 pip에서 모듈 추가
pip install git+https://github.com/dpallot/simple-websocket-server.git
샘플 코드 작성
testapp.py
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
class SimpleEcho(WebSocket):
def handleMessage(self):
# echo message back to client
self.sendMessage(self.data)
def handleConnected(self):
print(self.address, 'connected')
def handleClose(self):
print(self.address, 'closed')
server = SimpleWebSocketServer('', 8000, SimpleEcho)
server.serveforever()
실행합니다.
python testapp.py
아무것도 표시되지 않지만 서버는 일어난 모양.
localhost:8000
에서 ws 서버가 시작됩니다.클라이언트측
샘플 코드 을 그대로 이용합니다.
index.html로 8001번 포트에서 서버를 시작합니다.
python -m SimpleHTTPServer 8001
http://localhost:8001
에 브라우저에서 액세스하면 이런 느낌의 페이지가 됩니다.
Connect
버튼을 누르면 8000번 포트의 ws 서버에 연결됩니다.
왼쪽이 ws 서버(8000번), 오른쪽이 web서버(8001번)입니다.
시그널링 해보자
여기까지 가능하면 나머지는 적용됩니다.
대원의 코드는 여기 핸즈온 자료의 내용입니다.
이번에는 여전히 이 오류가 발생합니다.

setRemoteDescription(answer) ERROR: DOMException: Failed to set remote answer sdp: Called in wrong state: STATE_INPROGRESS
Node.js의 시그널링 서버 샘플 에서는 객체 비교를 해 websocket 클라이언트가 같은지를 판정하고 있습니다만,
function isSame(ws1, ws2) {
// -- compare object --
return (ws1 === ws2);
}
Python 측에서 비교를 하는 것보다 클라이언트 측에서 UUID 발행 쪽이 빠르기 때문에 클라이언트 측에서 판정하도록 합니다.
이 논리를 파이썬으로 작성해 보겠습니다.
서버측
거의
testapp.py
의 내용과 동일하지만 signaling.py
를 작성했습니다.signaling.py
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
clients = []
class SimpleEcho(WebSocket):
def handleMessage(self):
for client in clients:
if client != self: #judge
print('-----on----')
client.sendMessage(self.data)
else:
print('----skip-----')
def handleConnected(self):
print(self.address, 'connected')
clients.append(self) #add client
def handleClose(self):
print(self.address, 'closed')
server = SimpleWebSocketServer('', 8000, SimpleEcho)
server.serveforever()
설명하면,
def handleConnected(self):
print(self.address, 'connected')
clients.append(self) #add client
여기에 새 액세스가 있을 때마다 클라이언트 정보를 기록합니다.
def handleMessage(self):
for client in clients:
if client != self: #judge
print('-----on----')
client.sendMessage(self.data)
else:
print('----skip-----')
앞에서 설명한 오류를 처리하기 위해 동일한 클라이언트의 정보를 건너 뜁니다.
클라이언트측
이쪽의 핸즈온의 내용 을 그대로 이용합니다.
끝에
이것으로 시그널링을 할 수 있었습니다. 코드는 여기입니다.
결과, 전혀 Python 만지지 않고 되어 버렸기 때문에, 적어도 서버 측에서 동일 클라이언트의 정보를 스킵 하는 처리를 쓸 수 있게 해서 업데이트하고 싶습니다.
파이썬으로 시그널링 서버를 만들 수있었습니다. 간단하네요.
Node.js의 코드와 비교해도 간단하게 쓸 수 없다고 감동하고 있습니다.

좀 더 시간을 만들어 Micro Python 만나고 싶다 ...
현장에서 이상입니다!
Reference
이 문제에 관하여(Python으로 WebRTC 시그널링 서버 만들기 #webrtckr), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/n0bisuke/items/952c55b9ce464039749a텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)