2017-02-07 6 views
0

Leute, Ich habe ein eckiges 2 Cli-Projekt. Es ist eine einfache Chat-Anwendung. Aus bestimmten Gründen empfängt/sendet der Server keine Nachricht an den Client. Es gibt keinen Kompilierungsfehler und App funktioniert, aber kein Socket Messaging. Unten ist der Code-Schnipsel aus jeder:Angular 2 Cli Projekt -Socket.io funktioniert nicht

Express:

const express = require('express'); 
const path = require('path'); 
const http = require('http'); 
const bodyParser = require('body-parser'); 


const app = express(); 

// Parsers for POST data 
    app.use(bodyParser.json()); 
    app.use(bodyParser.urlencoded({ extended: false })); 

// Point static path to dist 
app.use(express.static(path.join(__dirname, 'dist'))); 


    // Catch all other routes and return the index file 
    app.get('*', (req, res) => { 
    res.sendFile(path.join(__dirname, 'dist/index.html')); 
    }); 

/** 
    * Get port from environment and store in Express. 
    */ 
    const port = process.env.PORT || '3000'; 
app.set('port', port); 

/** 
* Create HTTP server. 
*/ 
const server = http.createServer(app); 

    //set socket.io for chat 
    var io = require('socket.io').listen(server); 
    io.on('connnection', (socket) => { 
    console.log('user connected'); 
    socket.on('message', (msg) => { 
    console.log('Message Received: ', msg); 
    socket.broadcast.emit('message', msg); 
    }); 
    socket.on('disconnect',() => { 
    console.log('user has disconnected'); 
    }); 


    }); 


    server.listen(port,() => console.log("server running")); 

App Komponente

import { Component } from '@angular/core'; 
import * as io from "socket.io-client"; 

@Component({ 
selector: 'app-root', 
templateUrl: './app.component.html', 
styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    messages: Array<String>; 
    chatBox: String; 
    socket: any; 
constructor() { 
    this.chatBox = ""; 
    this.socket = io("http://localhost:3000"); 
     this.socket.on("message", (msg) => { 
     this.messages.push(msg); 
     }); 
    } 
    send(message) { 
    this.socket.emit("message", message); 
    this.chatBox = ""; 
    } 

} 

Html:

<ul> 
    <li *ngFor="let item of messages">{{item}}</li> 
    </ul> 

<input [(ngModel)]="chatBox" autocomplete="off" /> 
    <button (click)="send(chatBox)">Send</button> 

ich Hilfe oder Hinweis schätzen würde dieses Problem zu beheben .

Wenn ich einfach Express basiert Server mit HTML-Chat funktioniert gut.

Danke

+0

Fehler wie zu verbinden oder etwas versäumt? –

+0

Einer meiner Arbeits Code .. https://gist.github.com/parthghiya/7a03cbf2bb5be5af7746e06a0d7d24fe –

+0

kein Fehler, immer nur keine console.log Nachricht – leo

Antwort

0

Es gibt keinen Code für die Verbindung zu Socket überhaupt.

ändern

this.socket = io ("http://localhost:3000");

zu

this.socket=io.connect("http://localhost:3000") 
+0

nicht helfen .... Wie haben Sie socket.io auf der Client-Seite gelesen? Haben Sie Skript auf angular-cli.json hinzugefügt oder setzen Sie in index.html oder Sie setzen in index.html – leo