How to use express request to send Socket.io or Sockjs?

I know this question is embarrassing, but the problem comes from the Samsung TV 2010/2011 SmartTV (and the blue ray, of course, the 2012 emulator works fine). I have ported simple examples of communication from the source and package to the SmartTV application. Both of them return to the JSONP poll, but from the SmartTV application, he can only release / click on the server. Receiving a message from the server can be several times without problems. After searching for an answer on the Samsung D forum (of course, there was nothing), I think the fastest way to get around this problem is to deploy the Express server by taking the post and JSON.parse data, then emit Socket.io/Sockjs from the inside inside the server itself.

Can someone show me a simple code example so that I can start from there? Many thanks.

I am creating code quickly, but it seems to not work:

Library / server.js

var express = require('express') , app = express.createServer() , io = require('socket.io').listen(app); app.listen(80); app.use(express.bodyParser()); app.get('/', function (req, res) { res.sendfile('/var/www/mpgs_lite_v3/index.html'); }); app.post('/', function(req, res){ console.log(req.body); io.sockets.emit('my other event', req.body); res.redirect('back'); }); io.sockets.on('connection', function (socket) { //socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); 

index.html

 <html> <head> <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script> </head> <body> <form method="post" action="/"> <input type="hidden" name="_method" value="put" /> <input type="text" name="user[name]" /> <input type="text" name="user[email]" /> <input type="submit" value="Submit" /> </form> </body> </html> 

"my other event" does not seem to receive anything.

+6
source share
4 answers

UPDATE: I updated the example so that you make it more complete. I used to have no app.listen , as well as a client side script that shows that it really works fine:

 <!doctype html> <html> <head> <script src="//www.google.com/jsapi"></script> <script src="/socket.io/socket.io.js"></script> <script>google.load("jquery", "1.7.1")</script> <script> var socket = io.connect("localhost", {port: 3000}); socket.on("foo", function(message) { console.log("foo: ", message) }); $(function() { $("button").click(function() { $.post("/foo", { message: $("input").val() }); }); }); </script> </head> <body> <input type=text>A message</input> <button>Click me!</button> </body> </html> 

And the server, now with the app.listen directive:

 var express = require("express"), app = express.createServer(), io = require("socket.io").listen(app) index = require("fs").readFileSync(__dirname + "/index.html", "utf8"); app.use(express.bodyParser()); app.get("/", function(req, res, next) { res.send(index); }); app.post("/foo", function(req, res, next) { io.sockets.emit("foo", req.body); res.send({}); }); app.listen(3000); 

Using:

 node app.js 

Go to http://localhost:3000/ and click the button. Check your console for an exit.

+12
source

Based on the example, SockJS express server.js might look like this:

  var express = require ('express');
 var sockjs = require ('sockjs');

 // 1. Echo sockjs server
 var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.2.min.js"};

 var sockjs_echo = sockjs.createServer (sockjs_opts);
 connections = {};
 sockjs_echo.on ('connection', function (conn) {
     console.log (conn.id);
     connections [conn.id] = conn
     conn.on ('close', function () {
         delete connections [conn.id];
     });

     // Echo.
     conn.on ('data', function (message) {
         conn.write (message);
     });
 });

 // 2. Express server
 var app = express.createServer ();
 sockjs_echo.installHandlers (app, {prefix: '/ echo'});

 console.log ('[*] Listening on 0.0.0.0:9999');
 app.listen (9999, '0.0.0.0');

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

 app.post ("/ send", function (req, res, next) {
     for (var id in connections) {
         connections [id] .write ('received POST');
     }
     res.send ({});
 });

To check the open browser on localhost: 9999 and run:

 curl localhost:9999/send -X POST 
+2
source

I don’t know if this will help, but you can make an abstraction on the client based on your browser, and then create a separate get function on the server that will process the request in the same way as socket.on Call me back. To find out where to send the information, I suggest that you use some key that can be stored in a hash table on the server and local storage on the client.

For client:

 var emit = function(event, options) { if ("WebSocket" in window) { socket.emit(event, options); console.log("emited via WebSocket"); } else { $.post("http://localhost/emit/" + event, options); console.log("emited via AJAX"); } } emit("echo", { key: localStorage.getItem("key"), data: { hello: "world" } }); socket.on("response", function(data) { console.log(data.hello); //will print "world" }); 

For server:

 var sockets = {}; var echo_handler = function(a) { var socket = sockets[a.key]; var data = a.data; socket.emit("response", data); } app.post("/emit/:event", function(req, res) { var event = req.params.event; switch (event) { case "echo": var a = { key: req.param("key"), data: req.param("data") } echo_handler(a); break; } }); io.sockets.on("connection", function(socket) { socket.on("connect", function(data) { sockets[data.key] = socket; }); socket.on("echo", echo_handler); }); 

Another way to do this would be to switch to Sockjs and use the patch.

If someone has a better solution for Socket.IO, it will be appreciated because I'm already deep in the project, and it's too late to switch Socket.IO for Sockjs, and I don't like this solution :(.

0
source

just delete this comment //socket.emit('news', {hello: 'world'}); to socket.emit ('news', {hello: 'world'});

it will work because it transmits data through the news, and you listen using my other event instead of β€œnews”, or you can just listen using β€œmy other event”

0
source

Source: https://habr.com/ru/post/909170/


All Articles