2017-02-24 79 views
0

我想用Websockets稍微擺弄一下。我安裝了一個紅寶石的寶石稱爲「WebSocket的-紅寶石」(https://github.com/imanel/websocket-ruby)我開始撬/ IRB會話並鍵入:示例JavaScript無法連接到Ruby WebSocket服務器

require "websocket" 
@handshake = WebSocket::Handshake::Server.new(:host => "localhost", :port => 8080,:secure=>true) 

以此爲據我所知啓動的WebSocket服務器。然後我在瀏覽器中打開JavaScript的HTML網頁,其中試圖連接到服務器:

<!doctype html> 
<html lang="en"> 
<head> 
    <title>Websocket Client</title> 
</head> 
<body> 
    <script> 
    var exampleSocket = new WebSocket("wss://localhost:8080"); 
    exampleSocket.onopen = function (event) { 
     exampleSocket.send("Can you hear me?"); 
    }; 
    exampleSocket.onmessage = function (event) { 
     console.log(event.data); 
    } 
    </script> 
</body> 
</html> 

但它在控制檯日誌中說:

failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED 

我想無論是在服務器,並在不同的端口客戶端分別爲:8081,12345,但我總是得到這個錯誤信息。

回答

0

我對websocket和javascript有一些想法,但不是websocket-ruby。 我希望這會對你有所幫助。

在...的NodeJS文件server.js,下面寫代碼

var WebSocketServer = require("ws").Server; 

var wss = new WebSocketServer({port:8100}); 

console.log("websocket Server is Running..."); 

wss.on('connection', function connection(ws) { 
    // Store the remote systems IP address as "remoteIp". 
    var remoteIp = ws.upgradeReq.connection.remoteAddress; 
    // Print a log with the IP of the client that connected. 
    console.log('Connection received: ', remoteIp); 
    // Add a listener which listens for the "message" event. 

    // When a "message" event is received, take the contents 
    // of the message and pass it to the broadcast() function. 
    ws.on('message', wss.broadcast); 
}); 

wss.broadcast = function(msg) { 
    wss.clients.forEach(function each(client) { 
     client.send(msg); 
    }) 
}; 

在javascript中......

var SERVER_URL = 'ws://localhost:8100'; 
     //instead of localhost you can also use IP address of your system 
var ws; 

    function connect() { 
     alert('connect'); 
     ws = new WebSocket(SERVER_URL, []); 
     // Set the function to be called when a message is received. 
     ws.onmessage = handleMessageReceived; 
     // Set the function to be called when we have connected to the server. 
     ws.onopen = handleConnected; 
     // Set the function to be called when an error occurs. 
     ws.onerror = handleError; 
    } 

    function handleMessageReceived(data) { 
     // Simply call logMessage(), passing the received data. 
     logMessage(data.data); 
    } 

    function handleConnected(data) { 
     // Create a log message which explains what has happened and includes 
     // the url we have connected too. 
     var logMsg = 'Connected to server: ' + data.target.url; 
     // Add the message to the log. 
     logMessage(logMsg) 
    } 

    function handleError(err) { 
     // Print the error to the console so we can debug it. 
     console.log("Error: ", err); 
    } 

    function logMessage(msg) { 
     // with the new message. 
     console.log(msg); 
    } 

    /** This is the scope function that is called when a users hits send. */ 
    function sendMessage{ 
     ws.send(msg); 
    }; 
connect(); 

在HTML中使用一個按鈕,發送消息的WebSocket服務器

<button onclick="sendMessage('Hi Websocket')">send message</button> 
+0

Thx,但是什麼是$範圍?控制檯說「$範圍未定義」。它是JQuery嗎? – Konstantin

+0

我現在編輯過... plz檢查一次。如果您有任何疑問,請隨時提問 –

0

據我所知,您所呈現的Ruby代碼確實是而不是啓動Websocket 服務器 ...它所做的是啓動服務器端解析器

要啓動服務器,您需要使用實際的websocket服務器。

ActionCable(with Rails)使用websocket-ruby庫來解析websocket事件,它使用nio4r來操作實際的服務器。

Faye有一個類似的解決方案和em-websockets使用EventMachine的websocket-ruby寶石。

其他Ruby Websocket服務器包括Iodine,它使用C庫facil.io。 Iodine由框架plezi以及獨立使用。

既然你試圖運行回聲服務器,這裏是一個使用Plezi框架一個簡單的例子(你可以把它作爲西納特拉或者Rails中間件)...

...放在一個config.ru以下文件:

require 'plezi' 
class WebsocketSample 
    # HTTP index 
    def index 
    'Hello World!' 
    end 

    # called when Websocket data is recieved 
    # 
    # data is a string that contains binary or UTF8 (message dependent) data. 
    def on_message(data) 
    puts "Websocket got: #{data}" 
    write data 
    end 
end 
Plezi.route '/', WebsocketSample 
run Plezi.app 

要運行服務器,呼叫(忽略$標誌,它標誌着將該代碼作爲終端代碼):

$ iodine 

通知:碘需要BSD/Unix/Linux機器,如macOS,Ubuntu等。它不會在Windows上工作。

相關問題