# voxy socket & rtc
# 1.建立連線
測試連線位置: wss://webrtc-voxy.cfd888.info:8186
正式版: wss://chat.horofriend88.com:8185
接收訊息
| key | value | description | type |
| ---------------- | -------- | ------------- | ------ |
| action | link_on | 訊息類型 | string |
| content | 連線成功 | 回傳訊息 | string |
| link_id | 2 | 連線id | int |
| category | system | 聊天類型 | string |
| iceserver_config | 如下 | iceserver資訊 | json |
```
{
iceServers: {
urls: [
'stun:stun.l.google.com:19302'
],
username: 'ceshi',
credential: 'ceshi'
}
}
```
# 2.綁定會員id
傳送參數
| key | demovalue | type | note | must |
| --------- | --------- | ------ | ------------------ | ---- |
| action | bind | string | 訊息類型 | T |
| user_id | 1 | string | 自己的id | T |
| user_img | | string | 自己的大頭貼 | F |
| user_name | 訪客1 | string | 自己的暱稱 | F |
| link_id | 2 | int | 此次的連線id | T |
| to_userid | -1 | string | 通知所有人你上線了 | T |
接收訊息
| key | value | description | type |
| -------- | ------------------- | ------------ | ------ |
| action | bind | 訊息類型 | string |
| content | | 回傳訊息 | string |
| link_id | 2 | 連線id | int |
| time | 2020-11-07 09:00:00 | push時間 | string |
| list | 參照下面格式1 | 當前會員列表 | array |
| category | private | 聊天類型 | string |
# 3. 傳送訊息
| key | demovalue | type | note | must |
| ------------- | ----------------- | ------ | ---------------------- | ---- |
| action | say | string | 訊息類型 | T |
| type | 1 | string |1:文字、2:語音、3:圖片、4:影片、5:互動遊戲、6:小天使系統消息、7:動態留言、8:男觀看自動發圖文、9:女群發、10:女發文前自動發興趣(包含type=8)、11:禮物、12:聊天室私密照片、13:贈送活動物品 | F |
| user_id | 1 | string | 自己的會員id | T |
| user_img | img/profile_0.jpg | string | 自己的大頭貼 | F |
| user_name | 訪客1 | string | 自己的暱稱 | F |
| to_userid | 2 | string | 對方會員id | T |
| to_username | 訪客2 | string | 對方暱稱 | F |
| content | 你好嗎 | string | 本次發送訊息 | T |
| category | private | string | 聊天類型 | T |
| media | 請看下面格式2 | string | 媒體資料 | T |
| to_user_os_type | 1 | string | 對方的手機os | F |
| to_user_token | aabbabc | string | 對方的手機token | F |
| to_user_chat_pn | 1 | string | 對方的聊天設定是否開啟 | F |
| sensitive | 1 | string | 是否含有敏感字眼,1: 有, 0:沒有 | F |
ps:category 如果帶入default 是針對所有人進行交談,帶入id 則為私聊
* 聊天室引用回復 type = 7
* 女生不出現在對話框的系統代發引導訊息 type = 8
* 系統引導訊息 type = 9
接收訊息
| key | value | description | type |
| ------------- | ------------------- | ---------------------- | ------ |
| action | say | 訊息類型 | string |
| content | hello world | push訊息 | string |
| link_id | 2 | 連線id | int |
| time | 2020-11-07 09:00:00 | push時間 | string |
| list | 參照下面格式1 | 當前會員列表 | array |
| to_userid | 2 | 對誰使用密語 | string |
| to_userimg | 2 | 對方圖 | string |
| to_username | 2 | 對方名 | string |
| category | private | 聊天類型 | string |
| media | 請看下面格式2 | 媒體資料 | string |
### 格式2
```
{"file":[{"file_id":"540","file_url":"http://172.105.228.202/voxy/file/mode_1/20210406/606bdc5416dff.png"
,"file_length":"","file_preview":"","diamond":"","islock":false}]}
```
# 4. 心跳檢測
目前預設30秒檢查一次,如果沒ping則會被server斷線
| key | demovalue | type | note | must |
| ------- | --------- | ------ | ---------------- | ---- |
| action | ping | string | 心跳檢測用的類型 | T |
| link_id | 1 | string | 自己的link id | T |
# 5.webrtc 通話/視訊/遊戲
## 情境圖:

# 發起詢問是否接聽電話
## 對象:發送方
| key | demovalue | type | note | must |
| --------------- | ----------------- | ------ | -------------- | ---- |
| action | call_remote | string | 訊息類型 | T |
| user_id | 1 | string | 自己的會員id | T |
| user_img | img/profile_0.jpg | string | 自己的大頭貼 | T |
| user_name | 訪客1 | string | 自己的暱稱 | T |
| to_userid | 2 | string | 對方會員id | T |
| used_phone | 1 | int | 是否有意願接聽 | T |
| media_type | 1 | int | 1語音2視訊 | T |
| user_voice_fee | 1 | int | 語音費用(此次通話費率) | T |
| user_text_fee | 1 | int | 訊息費用(此次通話費率) | T |
| user_video_fee | 1 | int | 視訊費用(此次通話費率) | T |
| to_user_os_type | 1 | int | 對方手機os 作業系統;1:Android,2:iOS | T |
| user_age | 1 | int | 自己年紀 | T |
| to_user_token | 1 | string | 對方的token | T
| connection_mode | 0 | string | 發起模式(Ex:incomingCall(0) / GuessGame(1)) / LuckyWheel(2) | T |
ps1.發起方 used_phone = 1 詢問通話 ; used_phone = 0 取消詢問通話
ps2. 若為接收方,used_phone = 1 同意通話 ; used_phone = 0 拒絕通話
ps3.web在同意的當下 要先確認 mediaDevices 是否存在 ,否則之後流程會有問題
# 確認是否接聽
## 對象:接收方
| key | demovalue | type | note | must |
| --------- | -------------------- | ------ | -------------- | ---- |
| action | call_remote_callback | string | 訊息類型 | T |
| user_id | 1 | string | 自己的會員id | T |
| user_img | img/profile_0.jpg | string | 自己的大頭貼 | F |
| user_name | 訪客1 | string | 自己的暱稱 | F |
| to_userid | 2 | string | 對方會員id | T |
| used_phone| 1 | int | 是否雙方都已確認 | T |
| media_type | 1 | int | 1語音2視訊 | T |
| to_user_voice_fee | 1 | int | 語音費用 | F |
| to_user_text_fee | 1 | int | 訊息費用 | F |
| to_user_video_fee | 1 | int | 視訊費用 | F |
| to_user_token | 1 | string | 對方的token | T
| connection_mode | 1 | string | 發起模式(Ex:incomingCall(0) / GuessGame(1)) | T |
ps1 : 接收方 used_phone = 0 收到來電通知 ; used_phone = 3 拒絕通話;
used_phone = 2 對方忙線中 ; used_phone = 4 不存在此人
ps2 : 撥打方 used_phone = 1 接聽電話 ; used_phone = 3 拒絕通話 ; used_phone = 2 對方忙線中 ; used_phone = 4 不存在此人
# 雙方都確認後 開始RTC
# create_offer 並發client_offer 通知給對方
| key | demovalue | type | note | must |
| --------- | ----------------- | ------ | ---------------- | ---- |
| action | client_offer | string | 訊息類型 | T |
| user_id | 1 | string | 自己的會員id | T |
| user_img | img/profile_0.jpg | string | 自己的大頭貼 | F |
| user_name | 訪客1 | string | 自己的暱稱 | F |
| to_userid | 2 | string | 對方會員id | T |
| info | description物件 | string | localDescription | T |
## 文件太難寫附上demo的相關code

# RTC client_offer 回傳
| key | demovalue | type | note |
| ---- | --------------- | ------ | ---------------------- |
| action | client_offer | string | 訊息類型 |
| info | description物件 | string | 對方的localDescription |
# RTC 接收到client_offer後 create_answer 並發client_answer 通知給對方
| key | demovalue | type | note | must |
| --------- | ----------------- | ------ | ---------------- | ---- |
| action | client_answer | string | 訊息類型 | T |
| user_id | 1 | string | 自己的會員id | T |
| user_img | img/profile_0.jpg | string | 自己的大頭貼 | F |
| user_name | 訪客1 | string | 自己的暱稱 | F |
| to_userid | 2 | string | 對方會員id | T |
| info | description物件 | string | localDescription | T |

# RTC client_answer 回傳
| key | demovalue | type | note |
| ---- | ------------- | ------ | ------------ |
| action | client_answer | string | 訊息類型 |
| info | description物件 | string | 對方的localDescription |

# RTC client_candidate
| key | demovalue | type | note | must |
| --------- | ----------------- | ------ | ------------ | ---- |
| action | client_candidate | string | 訊息類型 | T |
| user_id | 1 | string | 自己的會員id | T |
| user_img | img/profile_0.jpg | string | 自己的大頭貼 | F |
| user_name | 訪客1 | string | 自己的暱稱 | F |
| to_userid | 2 | string | 對方會員id | T |
| ice_sdp | xxx | string | iceCandidate.sdp | T |
| ice_index | 0 |int|iceCandidate.sdpMLineIndex | T |
| ice_mid | 2 | string | iceCandidate.sdpMid | T |

# RTC client_candidate 回傳
| key | demovalue | type | note |
| --------- | ---------------- | ------ | -------------------------- |
| action | client_candidate | string | 訊息類型 |
| ice_sdp | xxx | string | iceCandidate.sdp |
| ice_index | 0 | int | iceCandidate.sdpMLineIndex |
| ice_mid | 2 | string | iceCandidate.sdpMid |
| logid | sfd123 | string | 本次通話的唯一id |

# 6.掛電話
| key | demovalue | type | note | must |
| --------- | ----------------- | ------ | ------------ | ---- |
| action | cancel_phone | string | 訊息類型 | T |
| user_id | 1 | string | 自己的會員id | T |
| user_img | img/profile_0.jpg | string | 自己的大頭貼 | F |
| user_name | 訪客1 | string | 自己的暱稱 | F |
| to_userid | 2 | string | 對方會員id | T |
| time | 0 | string | 通話秒數 | T |