簡體   English   中英

如何使用nodejs和socket.io在php中實現私人聊天

[英]How to implement private chat in php using nodejs and socket.io

我用這個實現了聊天應用程序( https://github.com/jdutheil/nodePHP) ,但現在我想要兩個用戶之間的私聊,但我不知道如何實現它。請幫我解決以下問題。

當用戶登錄他的帳戶時,它將列出(使用帶有朋友姓名的超鏈接)他的朋友,例如Facebook,其中有一個與其關聯的唯一ID,在點擊每個項目時,它會打開帶有文本框和按鈕的新聊天頁面並開始聊天。這里。是代碼

啟動聊天與-friends.php

<?php
  //uniqueid of the friend
  $id=$_GET['id'];
?>
<form class="form-inline" id="messageForm">
                <input id="messageInput" type="text" class="input-xxlarge" placeHolder="Message" />
                <input type="submit" class="btn btn-primary" value="Send" />
            </form>

chatclient.js

$( "#messageForm" ).submit( function() {
var msg = $( "#messageInput" ).val();
socket.emit('join', {message:msg} );
    $( "#messageInput" ).val('');
});



chatServer.js**

socket.on('join', function( data ) {

io.sockets.emit('new_msg'+data.to,{message:data.message});
    });

嗨,這可能對你有所幫助

var users = {};
var sockets = {};

io.sockets.on('connection', function(socket) {

// Register your client with the server, providing your username
socket.on('init', function(username) {
    users[username] = socket.id;    // Store a reference to your socket ID
    sockets[socket.id] = { username : username, socket : socket };  // Store a reference to your socket
});

// Private message is sent from client with username of person you want to 'private message'
socket.on('private message', function(to, message) {
    // Lookup the socket of the user you want to private message, and send them your message
    sockets[users[to]].emit(
        'message', 
        { 
            message : message, 
            from : sockets[socket.id].username 
        }
    );
});
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM