2015-02-03 5 views
5

मैं एक रेल वेबपृष्ठ विकसित कर रहा हूं जिसे बाहरी रूबी क्लाइंट के साथ संवाद करने के लिए वेबस्केट कार्यक्षमता का उपयोग करने की आवश्यकता है। ऐसा करने के लिए, मैं वेबस्केट-रेल रेल सर्वर में मणि का उपयोग कर रहा हूं, क्लाइंट_कनेक्टेड क्लाइंट_डिस्कनेक्टेड ईवेंट को परिभाषित करता हूं और क्लाइंट (new_message) से संदेश प्राप्त करने के लिए एक विशिष्ट क्रिया का उपयोग कर रहा हूं।वेबसाइकिल-रेल मणि के लिए रूबी websocket क्लाइंट

ग्राहक के पक्ष मैं जब मैं एक संदेश भेजने के लिए कोशिश की तरह फेय-WebSocket-माणिक और WebSocket-ग्राहक-सरल विभिन्न माणिक रत्न उपयोग करने के लिए लेकिन मैं हमेशा त्रुटियों प्राप्त की कोशिश की है पर। सर्वर पर मुझे इन संदेशों को संसाधित करने का तरीका नहीं मिल रहा है।

सर्वर साइड

: दोनों जवाहरात एक भेजें विधि केवल के साथ एक स्ट्रिंग

कोड है कि मैं का उपयोग किया गया (नहीं इस कार्यक्रम का नाम निर्दिष्ट करने के लिए सक्षम किया जा रहा) को स्वीकार करता है निम्नलिखित है एप्लिकेशन/नियंत्रक/chat_controller.rb

class ChatController < WebsocketRails::BaseController 
    def new_message 
    puts ')'*40 
    end 

    def client_connected 
    puts '-'*40 
    end 

    def client_disconnected 
    puts '&'*40 
    end 
end 

config/events.rb

WebsocketRails::EventMap.describe do 
    subscribe :client_connected, :to => ChatController, :with_method => :client_connected 

    subscribe :message, :to => ChatController, :with_method => :new_message 

    subscribe :client_disconnected, :to => ChatController, :with_method => :client_disconnected 
end 

config/initializers/websocket_rails.rb

WebsocketRails.setup do |config| 
    config.log_path = "#{Rails.root}/log/websocket_rails.log" 
    config.log_internal_events = true 
    config.synchronize = false 
end 

क्लाइंट साइड

WebSocket-ग्राहक-सरल

require 'rubygems' 
require 'websocket-client-simple' 

ws = WebSocket::Client::Simple.connect 'ws://localhost:3000/websocket' 

ws.on :message do |msg| 
    puts msg.data 
end 

ws.on :new_message do 
    hash = { channel: 'example' } 
    ws.send hash 
end 

ws.on :close do |e| 
    p e 
    exit 1 
end 

ws.on :error do |e| 
    p e 
end 

hash = { channel: 'Example', message: 'Example' } 
ws.send 'new_message', hash 

loop do 
    ws.send STDIN.gets.strip 
end 

फेय-WebSocket

require 'faye/websocket' 
require 'eventmachine' 

EM.run { 
    ws = Faye::WebSocket::Client.new('ws://localhost:3000/websocket') 

    ws.on :open do |event| 
    p [:open] 
    end 

    ws.on :message do |event| 
    p [:message, event.data] 
    end 

    ws.on :close do |event| 
    p [:close, event.code, event.reason] 
    ws = nil 
    end 

    ws.send('Example Text') 
} 

अग्रिम धन्यवाद। सादर।

पीडी: अगर आपको और कोड चाहिए तो मुझे बताएं।

उत्तर

3

अंततः मुझे समाधान मिला है। समस्या यह थी कि वेबसाकेट-रेल द्वारा समझने के लिए संदेश को certain format के साथ बनाया जाना आवश्यक है।

उदाहरण: ws.send ('[ "new_message", { "डेटा": "उदाहरण संदेश"}]')

जहां new_message घटना है जो WebSocket-रेल सुन रहा है है।

संबंधित मुद्दे