简体   繁体   中英

Ice::BadMagicException between C++ Server and Javascript browser client

I tried ICE by building the cpp hello world project (server & client) and it worked perfectly. I now have a cpp server

Server

    try
    {
        Ice::CommunicatorHolder ich(argc, argv);
        auto adapter = ich->createObjectAdapterWithEndpoints("OnlineJudgeAdapter", "default -p 10000");
        auto servant = make_shared<ComsI>();
        adapter->add(servant, Ice::stringToIdentity("OnlineJudge"));
        adapter->activate();
        ich->waitForShutdown();
    }
    catch(const std::exception& e)
    {
        cerr << e.what() << endl;
        return 1;
    }

which when compiled and ran, seems to work. I run using --Ice.Trace.Network=2 flag so I see the network traffic.

My client is in Javascript in the browser. I followed the guide to get to:

Client

        try
        {
            const hostname = document.location.hostname || "127.0.0.1";
            const proxy = communicator.stringToProxy(`OnlineJudge:ws -h 127.0.0.1 -p 10000`);
            const judge = await OJMod
                .ComsPrx
                .checkedCast(proxy);

            if (judge) {
                await judge.listProblems();
            } else {
                $("#output").val("Invalid proxy");
            }
        } catch (ex) {
            console.log("NOPE");
            $("#output").val(ex.toString());
        }

HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ice/3.7.3/Ice.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ice/3.7.3/Glacier2.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ice/3.7.3/IceStorm.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ice/3.7.3/IceGrid.js"></script>
<script type="text/javascript" src="OnlineInterface.js"></script>
<script type="text/javascript" src="Client.js"></script>

The client is csac.php and ran using: php -S localhost:8000/csac.php

I am getting the following errors

On Client:

::Ice::ConnectFailedException
  ice_cause:  
    isTrusted: "true"  
    wasClean: "false"  
    code: "1006"  
    reason: ""  
    type: "close"  
    target:    
      URL: "ws://127.0.0.1:10000/"    
      url: "ws://127.0.0.1:10000/"    
      readyState: "3"    
      bufferedAmount: "0"    
      onerror: (null)    
      protocol: ""    
      extensions: ""    
      binaryType: "arraybuffer"    
      CONNECTING: "0"    
      OPEN: "1"    
      CLOSING: "2"    
      CLOSED: "3"  
    currentTarget: (recursive)  
    eventPhase: "2"  
    cancelBubble: "false"  
    bubbles: "false"  
    cancelable: "false"  
    defaultPrevented: "false"  
    composed: "false"  
    timeStamp: "4549"  
    srcElement: (recursive)  
    returnValue: "true"  
    NONE: "0"  
    CAPTURING_PHASE: "1"  
    AT_TARGET: "2"  
    BUBBLING_PHASE: "3"
  line: "975"
  column: "22"
  sourceURL: "https://cdnjs.cloudflare.com/ajax/libs/ice/3.7.3/Ice.js"
  error: "1006"

On Server (from network trace):

-- 03/31/20 15:06:36.123 ./judge: Network: trying to accept tcp connection
   local address = ::ffff:127.0.0.1:10000
   remote address = ::ffff:127.0.0.1:52339
-- 03/31/20 15:06:36.124 ./judge: Network: accepted tcp connection
   local address = ::ffff:127.0.0.1:10000
   remote address = ::ffff:127.0.0.1:52339
-- 03/31/20 15:06:36.124 ./judge: Network: closed tcp connection
   local address = ::ffff:127.0.0.1:10000
   remote address = ::ffff:127.0.0.1:52339
   src/Ice/ConnectionI.cpp:1676: ::Ice::BadMagicException:
   unknown magic number: 0x47, 0x45, 0x54, 0x20

What is a BadMagicException? Is there a version conflict or am I passing the proxy string incorrectly?

After some digging I found that you need to enabled ws support on the server as well. This can be done using the --Ice.Default.Protocol=ws flag.

./judge --Ice.Default.Protocol=ws fixes the issue.

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