Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ubuntu 16.04 REP/REQ Over websockets question #193

Open
LionCoder4ever opened this issue Jan 22, 2018 · 6 comments
Open

Ubuntu 16.04 REP/REQ Over websockets question #193

LionCoder4ever opened this issue Jan 22, 2018 · 6 comments
Labels

Comments

@LionCoder4ever
Copy link

LionCoder4ever commented Jan 22, 2018

here is my server code:

const nanomsg = require('nanomsg');
const http = require('http');
const fs = require('fs');
const rep = nanomsg.socket('rep')
rep.bind('ws://127.0.0.1:7789');
rep.on('data', function(msg){
    console.log(msg)
    rep.send('received message');
})
http.createServer(function( req, res ){
  fs.createReadStream( 'index.html' ).pipe( res )
}).listen( 3000 )

and my front code:

<!DOCTYPE html>  
<html>  
<head></head>  
<body>  
   <div id="response"> 
      <span> </span> 
   </div> 
   <input type="text"> 
   <button>send</button> 
   <script> 

      var ws = new WebSocket('ws://127.0.0.1:7789',[
         'rep.sp.nanomsg.org'
      ])

      ws.onmessage = function( e ){
          var reader = new FileReader() // handle binary messages
          reader.addEventListener('loadend', function(){
             var result = reader.result;
             document.querySelector('span').textContent = result
          });
          reader.readAsText( e.data );
      }

       var button = document.querySelector('button');
       var input  = document.querySelector('input');
       button.addEventListener('click', function( e )
       {
           ws.send(input.value);
           input.value = '';
       })

   </script> 
</body>  
</html>  

from the chrome devtools I see the upload msg,but the server client doesn't console or reply the msg .After that, i change the req/rep to pair and pub/sub, it works. Is there anything wrong in my req/rep code? Hope your help,Best wishes

@LionCoder4ever
Copy link
Author

@reqshark could you help me solve the issue?

@reqshark
Copy link
Collaborator

hi @LionCoder4ever, yes happy to help

I will look into this tomorrow morning and we'll get to the bottom of it

thanks for your patience :)

@reqshark
Copy link
Collaborator

@LionCoder4ever I'm not sure if req/rep protocol works with websockets in the browser.

I was able to receive a req message in the browser by changing the server in your example code to req and inserting req.sp.nanomsg.org as the argument to the websocket constructor but I wasn't able to receive any further messages

@LionCoder4ever
Copy link
Author

@reqshark follow your test, i tried and also can't receive any further messages. Maybe it just not support work in websocket.
Howerver,thanks to your help! Have a nice day!

@reqshark
Copy link
Collaborator

btw @LionCoder4ever if you're serious about using nanomsg pub/subs for websocket in the browser, I recommend using the wsopt() socketopt

seems like you already figured out that pair and req/rep sockets are useless for doing websocket msgs over the browser in nanomsg

and avoid messing with browser's FileReader to decode, b/c text frames are more convenient.

here's an update to server.js that we should probably fix in examples/ws:

const addr = 'ws://' + '127.0.0.1' // or do: + require('ip').address()
const pub = require('nanomsg').socket('pub')
const sub = require('nanomsg').socket('sub')

/* set the nanomsg websocket text frames option */
pub.wsopt('text')
sub.wsopt('text')

/* use case doesnt need buffers, set stream encoding to utf8 */
sub.setEncoding('utf8')

/* websocket connections with some random ports */
pub.bind(addr + ':7789')
sub.bind(addr + ':7790')

/* recv websocket msgs */
sub.on('data', recv)

function recv(msg) {
  console.log(msg)
  pub.send(`${msg} ... from node-nanomsg`)
}

/* delete/replace this with your browser server code */
require('http')
  .createServer(function( req, res ){
    require('fs')
      .createReadStream( 'index.html' )
      .pipe( res )
  })
  .listen( 3000 )

console.log('please check http://localhost:3000')

now with text frames sockopts set, your index.html javascript is a bit more reasonable:

<!DOCTYPE html>
<html>
<head></head>
<body>
  <div class='res'><span></span></div>
  <input type='text' />
  <button>send</button>
  <script>
    var addr = 'ws://127.0.0.1'
    var sub = new WebSocket(addr + ':7789', [ 'pub.sp.nanomsg.org' ])
    var pub = new WebSocket(addr + ':7790', [ 'sub.sp.nanomsg.org' ])

    /* recv websocket msgs */
    sub.onmessage = recv

    function recv(msg) {
      document
        .querySelector('span')
        .textContent = msg.data /* <-- an improvement from FileReader :) */
    }

    /* send websocket msgs w/ input button */
    var input = document.querySelector('input')
    document
      .querySelector('button')
      .addEventListener('click', send)

    function send( e ){
      pub.send(input.value)
      input.value = ''
    }

    /* call send() also if user hits return key */
    input.onkeydown = function hitreturn ( e ) {
      if (e.keyCode === 13 || e.which === 13)
        send()
    }
  </script>
</body>
</html>

@LionCoder4ever
Copy link
Author

@reqshark thanks for your recommend advice , i solved my problem successfully!
Best wishes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants