اتصال به WebSocket Server
const socket = io('https://coinceeper.com', {
auth: {
user_id: 'YOUR_USER_ID'
},
transports: ['websocket', 'polling']
});
socket.on('connect', () => {
console.log('Connected!');
});ورود به یک مکالمه
| فیلد | نوع | توضیحات |
|---|---|---|
| user_id | string (UUID) | الزامی شناسه کاربر |
| conversation_id | string (UUID) | الزامی شناسه مکالمه |
socket.emit('join_conversation', {
user_id: 'uuid',
conversation_id: 'uuid'
});socket.on('joined_conversation', (data) => {
console.log('Joined:', data.conversation_id);
});ارسال پیام لحظهای
| فیلد | نوع | توضیحات |
|---|---|---|
| sender_id | string (UUID) | الزامی شناسه فرستنده |
| receiver_id | string (UUID) | الزامی شناسه گیرنده |
| message | string | الزامی متن پیام |
| message_type | string | اختیاری نوع پیام (پیشفرض: text) |
socket.emit('send_message', {
sender_id: 'uuid',
receiver_id: 'uuid',
message: 'سلام!',
message_type: 'text'
});socket.on('new_message', (data) => {
console.log('New message:', data);
if (data.is_edited) {
console.log('(edited)');
}
});شروع تایپ (نمایش typing indicator)
| فیلد | نوع | توضیحات |
|---|---|---|
| user_id | string (UUID) | الزامی شناسه کاربر |
| conversation_id | string (UUID) | الزامی شناسه مکالمه |
socket.emit('typing_start', {
user_id: 'uuid',
conversation_id: 'uuid'
});socket.on('user_typing', (data) => {
if (data.is_typing) {
showTypingIndicator(data.user_id);
} else {
hideTypingIndicator(data.user_id);
}
});توقف تایپ (مخفی کردن typing indicator)
| فیلد | نوع | توضیحات |
|---|---|---|
| user_id | string (UUID) | الزامی شناسه کاربر |
| conversation_id | string (UUID) | الزامی شناسه مکالمه |
socket.emit('typing_stop', {
user_id: 'uuid',
conversation_id: 'uuid'
});ویرایش پیام
| فیلد | نوع | توضیحات |
|---|---|---|
| user_id | string (UUID) | الزامی شناسه کاربر (باید فرستنده باشد) |
| message_id | string (UUID) | الزامی شناسه پیام |
| message | string | الزامی متن جدید پیام |
socket.emit('edit_message', {
user_id: 'uuid',
message_id: 'uuid',
message: 'پیام ویرایش شده'
});socket.on('message_edited', (data) => {
updateMessageInUI(data);
});حذف پیام
| فیلد | نوع | توضیحات |
|---|---|---|
| user_id | string (UUID) | الزامی شناسه کاربر (باید فرستنده باشد) |
| message_id | string (UUID) | الزامی شناسه پیام |
socket.emit('delete_message', {
user_id: 'uuid',
message_id: 'uuid'
});socket.on('message_deleted', (data) => {
removeMessageFromUI(data.message_id);
});دریافت خطا از سرور
socket.on('error', (data) => {
console.error('Error:', data.message);
});import io from 'socket.io-client';
const socket = io('https://coinceeper.com', {
auth: { user_id: 'YOUR_USER_ID' }
});
socket.on('connect', () => {
socket.emit('join_conversation', {
user_id: 'USER_ID',
conversation_id: 'CONVERSATION_ID'
});
});
socket.on('new_message', (data) => {
displayMessage(data);
if (data.is_edited) {
showEditedTag(data.message_id);
}
});
socket.on('user_typing', (data) => {
if (data.is_typing) {
showTypingAnimation(data.user_id);
} else {
hideTypingAnimation(data.user_id);
}
});
const input = document.getElementById('message-input');
input.addEventListener('input', () => {
socket.emit('typing_start', {
user_id: 'USER_ID',
conversation_id: 'CONVERSATION_ID'
});
clearTimeout(typingTimeout);
typingTimeout = setTimeout(() => {
socket.emit('typing_stop', {
user_id: 'USER_ID',
conversation_id: 'CONVERSATION_ID'
});
}, 1000);
});