0

Client is perfect on my computer(server on com), but rasp is not. Not thing happend when i runs it. Give me some idea, please!

Server:

var express = require("express");
var app = express();
var server = require("http").Server(app); 
var io = require("socket.io")(server);

server.listen(3000, function(){
  console.log("Server is running...3000.");
})

app.use(express.static("public"));
app.set("view engine", "ejs");
app.set("views", "./views");

app.get("/", function(req, res){
    res.render("index");
});

io.sockets.on("connection", function(socket){
    console.log(socket.id + " connected");

    socket.on("disconnect", function(){
        console.log(socket.id + " disconnected");
    });

    socket.on("client_send_data", function(data){
        console.log(data);

    })
    io.sockets.emit("server_send_data", 1);

})

Client

var socket = require('socket.io-client')('http://localhost:3000');
socket.on('server_send_data', function(data){
   console.log("connected: " + data);
});
socket.emit('client_send_data', "hello");

1 Answer 1

0

I'd like to second this question. My code works perfect on my linux laptop, but when I move the same code to RPi (with nodejs installed), it doesn't work:

const io = require('socket.io-client')
const socket = io.connect('http://192.168.0.6:1666', { reconnect: true });

socket.on('connect', function () {
    console.log("socket connected"); 
});

socket.on('broadcast', function (data) {
    console.log("we got a broadcast: ", data);
});

I installed express, socket.io, socket.io-client, and serialport, though I believe socket.io-client is the only one I need here.

WAIT!! I am adding the following postscript. Because, 20 minutes after I wrote the above question, the socket popped up. Now it's working.

So, it's good that it worked eventually, but still a question remains - is there something about the Raspberry Pi that it struggles to make a socket connection where a linux laptop could do it with no problem? Not sure why after 20 minutes my sockets are now working.

1
  • Note that you're not supposed to post answers to bump up a question. Answers are expected to well, answer the question being asked. If you earn some reputation here, you'll be able to vote up questions which you like to attract more attention to them. Apr 3, 2017 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.