简体   繁体   English

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

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

I have implemented chat application using this ( https://github.com/jdutheil/nodePHP),but now i want private chat between two users,but i don't know how to implement it.Please help me to solve the following issue. 我用这个实现了聊天应用程序( https://github.com/jdutheil/nodePHP) ,但现在我想要两个用户之间的私聊,但我不知道如何实现它。请帮我解决以下问题。

When a user logged into his account it will list(using hyperlinks with friends name) his friends like Facebook with a unique id associate with it,on clicking each item it opens new chat page with a text box and a button and start chat.Here is the code 当用户登录他的帐户时,它将列出(使用带有朋友姓名的超链接)他的朋友,例如Facebook,其中有一个与其关联的唯一ID,在点击每个项目时,它会打开带有文本框和按钮的新聊天页面并开始聊天。这里。是代码

start-chat-with-friends.php 启动聊天与-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 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});
    });

hi root this might be helpful for u 嗨,这可能对你有所帮助

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