ga-wdi-exercises / project4

[project]
https://github.com/ga-wdi-exercises/project4
2 stars 8 forks source link

app not reading my websockets #468

Closed Nykke closed 7 years ago

Nykke commented 7 years ago

I'm not getting anything in my server (my console logs) and I'm currently working on making it angular readable but shouldn't I be able to create my messages as is?

this is index.js

var server = require("http").createServer(app);
var io = require("socket.io").listen(server);

users = [];
connections = [];

/connecting to angular
app.get("/", function(req, res) {
  res.sendFile(__dirname + "/index.html")
})

//sockets connection
io.sockets.on("connection", function(socket){
  connections.push(socket);
  console.log("Connected: %s sockets connected", connections.length)
  //disconnections
  socket.on("disconnect", function(data){
    users.splice(users.indexOf(socket.username), 1)
    connections.splice(connections.indexOf(socket), 1)
    console.log("Disconnected: %s sockets connected", connections.length)
  })
})

//send chat messages
var socket = io;
socket.on("send message", function(data){
  io.sockets.emit("new message", {msg: data, user: socket.username});
  console.log(data)
})

//new user
socket.on("new user", function(data, callback){
  callback(true);
  socket.username = data;
  users.push(socket.username);
  updateUsernames()
})

function updateUsernames(){
  io.sockets.emit("get users", users)
}
superbuggy commented 7 years ago

It seems that the issue is here:


//send chat messages
var socket = io;
socket.on("send message", function(data){
  io.sockets.emit("new message", {msg: data, user: socket.username});
  console.log(data)
})
//send chat messages

///move your emit somewhere outside of the socket listener. the event names should match also
  io.sockets.emit("message", {msg: data, user: socket.username});

var socket = io;
socket.on("message", function(data){
  console.log(data)
})
Nykke commented 7 years ago

just tried doing that and the form is still not taking the input.

superbuggy commented 7 years ago

Do you this line inside the form submission handling function?

  io.sockets.emit("message", {msg: data, user: socket.username});
Nykke commented 7 years ago

yes, I have a similar line

  $messageForm.submit(function(e){
      e.preventDefault()
      console.log(submitted)
      socket.emit("send message", $message.val())
      $message.val(" ")
    })
superbuggy commented 7 years ago

change socket.emit("send message") to socket.emit("message"). the first argument to emit and on is a string naming the event type (the strings must match in order for on to listen for the same event being emitted). "send message" is therefore assumed to be a different type of user-defined event than "message" or "new message".

Nykke commented 7 years ago

it didn't work, sorry I don't understand what's going

superbuggy commented 7 years ago

emit("message", /*callback function here*/) emits an event that will trigger the callback inside of on("message", , /*callback function here*/)

Nykke commented 7 years ago

would you mind taking a look at my branch? I get no errors and it's just not taking the value https://github.com/Nykke/project_04/tree/websocket

superbuggy commented 7 years ago

what value do you mean?

Nykke commented 7 years ago

the input that the user is entering into the form

superbuggy commented 7 years ago

it's because this line of code is stranded outside the route that handles form submissions

  io.sockets.emit("message", {msg: data, chatter: socket.username});

you'll have to adapt it though to the data available in available in your post handler:

app.post("/api/maintenance_requests", function(req, res){
  Maintenance_Request.create(req.body).then(function(maintenance_request){
    io.sockets.emit("message", {msg: maintenance_request});
    res.json(maintenance_request);
  });
})
Nykke commented 7 years ago

I'm calling it quits, it didn't work. thanks for your help