Advertisement
vitareinforce

sample RMQ

Apr 7th, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. using RabbitMQ.Client;
  6. using RabbitMQ.Client.Events;
  7.  
  8. public class Test : MonoBehaviour
  9. {
  10. public ConnectionFactory connectionFactory;
  11. public IConnection connection;
  12. public IModel channel;
  13. private bool isReceiving = false;
  14. string host = "cloudrmqserver.pptik.id";
  15. int port = 5672;
  16. string user = "tmdgdai";
  17. string pass = "tmdgdai";
  18. string vhost = "/tmdgdai";
  19. string queue_name = "TestUnity";
  20.  
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. connectionFactory = new ConnectionFactory();
  25. connectionFactory.HostName = host;
  26. //connectionFactory.Port = port;
  27. connectionFactory.UserName = user;
  28. connectionFactory.Password = pass;
  29. connectionFactory.VirtualHost = vhost;
  30.  
  31. connection = connectionFactory.CreateConnection();
  32. Debug.Log("Koneksi " + (connection.IsOpen ? "Berhasil!" : "Gagal!"));
  33. }
  34.  
  35. // Update is called once per frame
  36. void LateUpdate()
  37. {
  38. using (channel = connection.CreateModel())
  39. {
  40. channel.QueueDeclare(queue: queue_name,
  41. durable: true,
  42. exclusive: false,
  43. autoDelete: false,
  44. arguments: null);
  45. var consumer = new EventingBasicConsumer(channel);
  46. consumer.Received += (model, ea) =>
  47. {
  48. Debug.Log("[x] Pesan diterima: " + Encoding.UTF8.GetString(ea.Body.ToArray()));
  49. };
  50. channel.BasicConsume(queue: queue_name,
  51. autoAck: true,
  52. consumer: consumer);
  53. }
  54. }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement