-
Notifications
You must be signed in to change notification settings - Fork 70
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
Comments
@reqshark could you help me solve the issue? |
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 :) |
@LionCoder4ever I'm not sure if req/rep protocol works with websockets in the browser. I was able to receive a |
@reqshark follow your test, i tried and also can't receive any further messages. Maybe it just not support work in websocket. |
btw @LionCoder4ever if you're serious about using nanomsg pub/subs for websocket in the browser, I recommend using the seems like you already figured out that and avoid messing with browser's here's an update to 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 <!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> |
@reqshark thanks for your recommend advice , i solved my problem successfully! |
here is my server code:
and my front code:
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
The text was updated successfully, but these errors were encountered: