Renê Silva Lima
Renê Silva Lima

Reputation: 2815

Socket Io not found 404

I'm trying to create a chat in Angular 2 using Socket IO based on this tutorial, however I'm getting this error message when I try to do a test on the server:

GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=LmD1N_Z 404 (Not Found)

The aplication is running on the port 3000 while the server is running on the port 8000 and I think this is why I'm getting this error. Is there a way that both server and aplication run on the same port?

Here's some code from the aplication: This is the component where I'm trying to do the test:

import { Component, OnInit} from '@angular/core';
import {Http, Headers} from '@angular/http';
import {Location} from '@angular/common';
import {Routes, RouterModule, Router} from '@angular/router';
import * as io from 'socket.io-client'

@Component({
  moduleId: module.id,
  selector: 'atendimento',
  templateUrl: `atendimento.component.html`
})

export class AtendimentoComponent implements OnInit{
  socket:SocketIOClient.Socket;
  constructor(private location: Location) { 
    this.socket = io.connect();
  }

  ngOnInit(){
    this.socket.emit('event1',{
      msg: 'Can you hear me? Over'
    });
    this.socket.on('event2', (data:any)=>{
      console.log(data.msg);
      this.socket.emit('event3', {
        msg: 'yes, its working for me'
      })
    })
    this.socket.on('event4', (data:any)=>{
      console.log(data.msg)
    })
  }

And this is the server.js where I make the connection with the server:

const express = require('express');
const path = require('path');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

const port = 8000;

app.use(express.static(path.join(__dirname, "/")));

io.on("connection", (socket) => {
    console.log('new connection made');


    socket.on('event1', (data) => {
        console.log(data.msg);
    })

    socket.emit('event2', {
        msg: 'Server to client, do you read me? Over'
    })

    socket.on('event3', (data) => {
        console.log(data.msg);
        socket.emit('event4', {
            msg: 'Loud and Clear :)'
        })
    })
})

server.listen(port, () => {
    console.log("Listening on port" + port);
})

Can someone help me? Thanks, let me know if you need more code.

Upvotes: 2

Views: 1405

Answers (1)

Gatsbill
Gatsbill

Reputation: 1790

Just connect socket io with the right server url :

replace :

this.socket = io.connect();

with :

this.socket = io('http://localhost:8000');

Upvotes: 2

Related Questions