简体   繁体   中英

Node.js, socket.io, require()

My target: Create node.js server and client applications that will send antyhing (event, string, function call) between two web browsers.

I installed and created simple server, but problems begin with creating a client. As said at http://socket.io/ I installed it by command:

npm install socket.io

but the file required in basic example:

<script src="/socket.io/socket.io.js"></script>

is not downloaded or installed. There is file named like that in /node_modules/socket.id/lib but is the same file ? I think that this is socket.io client but as module for server, not for web browser javascript right ?

How to get socket.io.js that just works and can connect to node.js server, without errors like io/socket/require is not defined etc. ?

I don't want to create web serwer that responds to normal http requests. I want server that will hold connection and able to communicate between JS scripts in two browsers

I just can stand it, that everybody writes about node.js in form like everybody on the world has socket.io.js file in every possible direcortory. The possible duplicate:

socket.io.js not found

Starts with a function call require() that IS NOT in clean JavaScript API. So i don't even try this sollution. Can anybody explain how to include and define io.* properly ? It is something about Express framework ? Why node.js and socket.id pages say nothing about express requirement ?

The problem is, that I need a CLIENT js library, and I don't mean "client on server", that my node binary server will have ability to connect to other pages/server and communicate. I mean client in HTML client page. And, if neccessary, other realted JS.

I grabbed a example from demo at http://serv1.aelag.com:8084/

/** Socket.IO 0.6.2 - Built with build.js */
/**
 * Socket.IO client
 * 
 * @author Guillermo Rauch <guillermo@learnboost.com>
 * @license The MIT license.
 * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
 */

this.io = {
    version: '0.6.2',
...

How to get that file in normal way and not to steal from other server (maybe unstable or old version) ? And I need exaclty that file. Are there any dependencies ? Should I create it with building it "build.js" or developers did that ?

I finally resolved this

The socket.io script doesn't exists as flat file. It is generated by node.js server and served to browser. So. including it by:

<script src="/socket.io/socket.io.js"></script>

Works only if node.js server is started on the same port as the web server with web application. If you try to load this script from for example apache on port 80, you need to put whole path to node.js server listen host:

<script src="http://localhost:8080/socket.io/socket.io.js"></script>

You do need to add /socket.io/socket.io.js anywhere. If you have installed socket.io via npm install socket.io your application knows how to handle it. I played around with socket.io myself a few months back and this is a very simple "chat" that I came up with and it worked. So it might help you out! (I am using ExpressJS. A very helpful Node.JS framework btw)

This is you server side application:

var app = require('express').createServer();
var io = require('socket.io').listen(app);

app.listen(3000);

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function (socket) {
    socket.on('news', function (data) {
        socket.emit('news', { content: data.text });
        socket.broadcast.emit('news', { content: data.text});
    });
});

This is your index.html

<!DOCTYPE html>
<html>
<head>
<title>socket test</title>
</head>
<body>
<input type="text" id="Username"> <input type="text" id="Content"><input type="button" value="send" onclick="sendMessage()"><br />
<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost:3000/pathtoproject');
  socket.on('news', function (data) {
    document.body.innerHTML += data.content + "<br />";
  });
  function sendMessage() {
      socket.emit('news', { text: document.getElementById('Username').value + ": " + document.getElementById('Content').value });
  }
</script>
</body>
</html>

I managed to get socket.io loaded on the client side like this:

jQuery.getScript("bower_components/socket.io/lib/socket.js", function() {
   var socket = io.connect('http://localhost');
   socket.on('news', function (data) {
     console.log(data);
     socket.emit('my other event', { my: 'data' });
   });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM