小编joh*_*yan的帖子

node.js / socket.io - 跟踪客户端

亲爱的朋友们,我在尝试跟踪聊天中的登录用户时遇到了一个小问题。聊天基于两个独立的频道,在命名空间的帮助下工作:

  1. chatInfra - 处理登录用户并发送欢迎消息。
  2. chatCom - 处理用户之间的消息。

我搜索了很多,但我只找到了理论上的解释,即最好的解决方案是将用户存储到数组中。因此,我试图通过将登录用户存储在数组中然后遍历它们来保持对登录用户的跟踪,但结果仍然不好。

问题是进入聊天后,屏幕上只显示第一个登录用户的名称,而看不到第二个用户的名称。

这是我的服务器端代码,我试图将用户存储到clients数组中:

    var clients = [];

    var chatInfra = io.of("/chat_infra").on("connection", function(socket){
        socket.on("set_name", function (data) {
          clients.push(data.name);
          socket.emit('name_set', data);
          socket.send(JSON.stringify({
             type:'serverMessage',  
             message:'Welcome!' 
         }));
        socket.broadcast.emit('user_entered', data);
        });
    });

    var chatCom = io.of("/chat_com").on("connection", function (socket) {
        socket.on('message', function (message) {
            message = JSON.parse(message);
            for(var key in clients){

               if(message.type == "userMessage"){
                  message.username = clients[key];
                  console.log('message : ', message);
                  socket.broadcast.send(JSON.stringify(message));
                  message.type = "myMessage";
                  socket.send(JSON.stringify(message));
               }
            }

        });
    });
Run Code Online (Sandbox Code Playgroud)

这是它在浏览器中的外观: http //screencast.com/t/lshnfcGZ8E8

这是完整的代码:https : //gist.github.com/johannesMatevosyan/0b9f7e588338dbb6b7f5

javascript json websocket node.js socket.io

6
推荐指数
1
解决办法
6268
查看次数

延迟加载时找不到模块错误

我想为我的模块设置延迟加载,但有一个我无法解决的错误。

错误是:

core.js:15724错误错误:未捕获(承诺中):错误:找不到模块“app/invoice-builder/invoice-builder.module”错误:找不到模块“app/invoice-builder/invoice-builder.module”

应用程序路由.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: 'invoice-builder',
    loadChildren : 'app/invoice-builder/invoice-builder.module#InvoiceBuilderModule'
  },
  {
    path: '**',
    redirectTo: 'invoice-builder'
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

应用程序模块.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import { …
Run Code Online (Sandbox Code Playgroud)

angular-routing angular-module angular

2
推荐指数
1
解决办法
3130
查看次数

Websockets:向除发件人以外的所有客户端发送消息和通知

我正在开发基于 websockets 和 webrtc 的聊天。我想向除发件人以外的所有连接用户发送消息,但我找不到合适的解决方案。更具体地说,我想向其他连接的用户发送新用户已加入聊天的通知。我试图为每个连接的用户提供一个唯一的 ID,但是每个新用户都会重写第一个分配的 ID,我无法区分用户。

服务器:

// list of users
var CLIENTS=[];
var id;

// web server is using 8081 port
var webSocketServer = new WebSocketServer.Server({ port: 8081 });

// check if connection is established
webSocketServer.on('connection', function(ws) {

id = Math.random();
CLIENTS[id] = ws;
CLIENTS.push(ws);

ws.on('message', function(message) {
    console.log('received: %s', message);
    var received = JSON.parse(message);

    if(received.type == "login"){
        ws.send(message);  // send message to itself

        /* *********************************************************** */
        /* *** Here I trying to check if message comes from …
Run Code Online (Sandbox Code Playgroud)

javascript json websocket node.js webrtc

0
推荐指数
1
解决办法
5289
查看次数