0

I have a flutter code and node backend. I want to communicate via socket. So I intiailize a socket client in flutter and socket server in node. But when I try to connect this client it creates an error in my node server that is

Error: Specified protocol was not requested by the client.

Flutter code

...

void initState() {
    super.initState();
    LogEntryRepository repository = new LogEntryRepository();
    getLogEntries(repository);
    var s = IOWebSocketChannel.connect("ws://192.168.0.107:8080/", protocols: ['echo-protocol']);
    print(s.protocol);
    print(s.closeReason);
}

...

Node Code

#!/usr/bin/env node
var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
    console.log((new Date()) + ' Received request for ' + request.url);
    response.writeHead(404);
    response.end();
});
server.listen(8080, "0.0.0.0", function() {
    console.log((new Date()) + ' Server is listening on port 8080');
});

wsServer = new WebSocketServer({
    httpServer: server,
    // You should not use autoAcceptConnections for production
    // applications, as it defeats all standard cross-origin protection
    // facilities built into the protocol and the browser.  You should
    // *always* verify the connection's origin and decide whether or not
    // to accept it.
    autoAcceptConnections: false
});

function originIsAllowed(origin) {
  // put logic here to detect whether the specified origin is allowed.
  return true;
}

wsServer.on('request', function(request) {
    if (!originIsAllowed(request.origin)) {
      // Make sure we only accept requests from an allowed origin
      request.reject();
      console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
      return;
    }

    var connection = request.accept('echo-protocol', request.origin);
    console.log((new Date()) + ' Connection accepted.');
    connection.on('message', function(message) {
        if (message.type === 'utf8') {
            console.log('Received Message: ' + message.utf8Data);
            connection.sendUTF(message.utf8Data);
        }
        else if (message.type === 'binary') {
            console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
            connection.sendBytes(message.binaryData);
        }
    });
    connection.on('close', function(reasonCode, description) {
        console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
    });
});

This is my Node code. The error occurs from the line request.accept('echo-protocol', request.origin);

1 Answer 1

2

It seems that IoWebSocketChannel ignores the protocols parameter.

Try constructing it as:

var ws = await WebSocket.connect("ws://192.168.0.107:8080/", protocols: ['echo-protocol']);
var s = IoWebSocketChannel(ws);

(You must do this in an async method as it requires await to get the underlying web socket.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. It worked. But why they have mentioned protocols as a paremeter if they simply ignored it.
I think it's a bug. As the workaround worked, that sort of proves it. Maybe create an issue for that package.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.