2016-09-10 43 views
1

我使用Ruby on Rails的上CLOUD9 IDE如何在Ruby on Rails中使用TCPServer?

$ ruby -v 
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux] 
$ rails -v 
Rails 4.2.4 

我不得不發送服務器和GPS設備之間的數據。我戴上了設備,並做了下面的代碼。

My_Controller

class DashboardController < ApplicationController 

    before_action :authenticate_user! 


     def index 

     require 'socket' 
     server = TCPServer.new ("127.0.0.1",8000) 
     loop do 
      Thread.start(server.accept) do |client| 
      client.puts "Hello !" 
      client.puts "Time is #{Time.now}" 
      client.close 
      end 
     end 
     @bookings = Booking.where(user: current_user).joins(:customer, :driver, :car) 
     @customers = Customer.where(user: current_user) 
     end 
end` **** 

,當我跑這我得到

<html> 
    <head> 
    <meta charset='utf-8'> 
    <title>Error 502 - Bad Gateway</title> 
    <link rel="stylesheet" type="text/css" href="https://cdn.c9.io/errors/style.css" /> 
    <style type="text/css"> 
    .error_content { 
     background: rgba(255, 255, 255, 0.23); 
     padding: 10px; 
     width: 641px; 
     margin: 25px 0; 
     display: none; 
    } 

    #error-msg { 
     display: block; 
    } 
    </style> 
    </head> 
    <body class="errorUnknown light"> 
    <div id="wrapper"> 
     <h1>Error 502 - Bad Gateway</h1> 
     <div class="error_content" id="error-msg"> 
      <p>Please click <a href="javascript:location.reload(true)">here</a> to try again, if the issue persists please contact <a href="https://c9.io/support">support</a></p> 
     </div> 

     <a href="http://status.c9.io">Status Page</a> | 
     <a href="https://c9.io/support">Support</a> | 
     <a href="https://c9.io/dashboard.html">Dashboard</a> | 
     <a href="https://c9.io">Home</a> 
    </div> 
    </body> 
</html> 

請,幫助我如何解決這個問題.. 在此先感謝。

+1

您的Rails應用程序已在TCP服務器上運行,否則您無法在瀏覽器中打開頁面。這意味着您嘗試在向另一個TCP服務器的請求中啓動TCP服務器。此外,該服務器運行在一個循環,永遠不會返回...我的建議是:關閉這個問題,並開始一個新的,並解釋你試圖達到什麼。什麼樣的數據需要傳送到什麼方向,你有什麼文件?這對我來說看起來像[xy問題](http://meta.stackexchange.com/a/66378/284887)。 – spickermann

+0

好的..我會做到的。如何查看我們的服務器(我們磚)從設備 –

+0

接收的數據(它是十六進制小數)這似乎是後續問題:https://stackoverflow.com/questions/39284765/ruby-on-rails-how -to-use-tcp-socket-to-connect-with-gps-device – spickermann

回答

2

你在行動中進入一個無限循環。從反向代理到您的Rails服務器的請求超時。反向代理向瀏覽器發送錯誤消息(502)。

+0

我刪除了循環..然後同樣的錯誤。 –

+1

然後沒有客戶端連接到127.0.0.1:8080和^ accept'call塊,我假設。 按照@ spickermann的建議,在你描述你想要達到的目標的地方打開一個新的問題。您目前的解決方案看起來破碎 –

+0

okay.please告訴我如何查看我們的服務器(我們磚)從GPS設備接收的數據(十進制十六進制) –