I am using paho-mqtt to receive data from emqx broker but I am losing data because sometimes my paho-mqtt subscriber goes down. Any method to get data when I will run subscriber again it will give all published data.
If there is no subscriber, EMQ x will discard the message, which is a normal design.
You can try EMQ x enterprise and use backend for offline data storage.
https://docs.emqx.io/tutorial/latest/en/backend/whats_backend.html
I hope you get a solution.
I don't know your client , but paho-mqtt java client can reconnect automatically. This reconnect can be check in "connectComplete(boolean reconnect, String s)" boolean parameter
public class MqttautoClient implements MqttCallbackExtended {
.
.
.
#Override
public void connectComplete(boolean reconnect, String s) {
Log.d(TAG, "Connection connectComplete");
}
#Override
public void connectionLost(Throwable throwable) {
Log.d(TAG, "Connection lost");
}
#Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
{
Log.d(TAG, "messageArrived");
}
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
Log.d(TAG, "deliveryComplete ");
}
}
Related
I want to make a socket between Java client
`
import java.util.*;
import java.net.*;
import java.io.*;
public class Main
{
public static void client()
{
final int port = 2003;
final String host = "localhost";
try
{
Socket soc = new Socket(host, port);
if(soc.isBound()){
System.out.println("socket binded");
}else if(soc.isConnected()){
Scanner in = new Scanner(soc.getInputStream());
System.out.println("enter msg");
String x = in.nextLine();
PrintStream p = new PrintStream(soc.getOutputStream());
p.println("#client" + x);
} else if(soc.isClosed()){
System.out.println("Closed tunnel");
System.exit(0);
soc.close();
}
}
catch (Exception e)
{
}
}
public static void main(String[] args)
{
client();
}
}
`
Its having problem allowing me to use the scanner to send msg to Python server{I dunno how to make this}
PS : am new and dunno how to make this snippets formatted
I am using Flask at the backend and ganache for my blockchain project. I am also using Metamask. My smart contract is on the remix website. And here is my contract:
pragma solidity ^0.4.21;
contract Election{
struct Candidate {
uint voteCount;
string name;
}
struct voter {
bool authorized;
bool voted;
uint vote;
}
address public owner;
string public electionName;
mapping(address => voter) public voters;
Candidate[] public candidates;
uint public totalVotes;
modifier ownerOnly() {
require(msg.sender == owner);
_;
}
constructor(string _name) public {
owner = msg.sender;
electionName = _name;
}
function addCandidate(string _name) ownerOnly public {
candidates.push(Candidate(0, _name));
}
function getNumCandidates() public view returns(uint) {
return candidates.length;
}
function authorize(address _person) ownerOnly public {
voters[_person].authorized = true;
}
function vote(uint _voteIndex) public {
require(!voters[msg.sender].voted);
require(voters[msg.sender].authorized);
voters[msg.sender].vote = _voteIndex;
voters[msg.sender].voted = true;
candidates[_voteIndex].voteCount += 1;
totalVotes += 1;
}
function end() ownerOnly public {
selfdestruct(owner);
}
}
I am facing a problem with Vote function. When I run this whole contract after deploying on the remix website it is working fine. But when I am making transaction from my flask based app there is revert error even after having the voter in Voters list. So using metamask I want to make my transaction for voting. I deploy the contract with a default account.
w3 = Web3(HTTPProvider('http://127.0.0.1:7545'))
w3.eth.defaultAccount = w3.eth.accounts[0]
contract = w3.eth.contract(address=address, abi=data["abi"])
tx_hash = contract.functions.vote(candidate_index).transact()
w3.eth.wait_for_transaction_receipt(tx_hash)
Now I want to make transactions from the account which I choose in Metamask. Because the above method is not working. Is there any way or any example to do it? I also visited metamask documentation where the use of ethereum.request() is suggested. But I was unable to implement it in web3py.
Your code is correct. There are 2 require statements in vote. I think that is why you are getting the error. You can add custom message to require functions so you can see which one is causing the error:
require(!voters[msg.sender].voted,"You already voted");
require(voters[msg.sender].authorized,"You are not authorized");
I believe since you already call vote with your default account, you are not passing the first require statement.
I'm running RabbitMQ, in a Docker container (rabbitmq:3-management image) as part of a Docker Compose application. The application contains some ASP.NET Core WebApi microservices, which exchange messages via this broker. That works fine and didn't give me any problems so far.
Now I need to publish messages from a Python application to an exchange/queue which was created from one of the ASP.NET Core microservices. The microservice contains a consumer for this queue. For publishing from python, I'm using pika. The problem is, I can't seem to get the publishing right. Whenever I execute my Python script, I can see in the RabbitMQ management UI that a new exchange and queue with the suffix "_skipped" were created. It seems as if my message was sent there instead of the actual queue. Also, when trying to publish directly from the management UI, the message actually makes it to the microservice, but there I'll get an exception, that the message could not be deserialized to a MassTransit envelope object, and also a new exchange and queue with the "_error" suffix.
I have no idea where the problem is. I think the exchange/queue themselves are fine, since other queues/consumers/publishers for microservice to microservice communication in this project work. So then it's probably either how I'm trying to address the exchange/queue from Python, or something with my message body which is not right.
This page gives some info about how messages need to be structured, but not too detailed, and here I got most of the info about how to publish with Python.
Below you see the relevant code regarding the host/queue configuration in the microservice, as well as the Python script. Any help/tips on how I can get this to work would be greatly appreciated.
ASP.NET Core:
// Declaring the host, queue "mappingQueue", consumer in Startup.ConfigureServices of microservice
...
services.AddMassTransit(x =>
{
x.AddConsumer<MappingUpdateConsumer>();
x.AddBus(provider => Bus.Factory.CreateUsingRabbitMq(config =>
{
config.Host(new Uri(RabbitMqConst.RabbitMqRootUri), h =>
{
h.Username(RabbitMqConst.RabbitMqUsername);
h.Password(RabbitMqConst.RabbitMqPassword);
});
config.ReceiveEndpoint("mappingQueue", e =>
{
e.ConfigureConsumer<MappingUpdateConsumer>(provider);
});
}));
});
services.AddMassTransitHostedService();
...
// Consumer
public class MappingUpdateConsumer : IConsumer<MappingUpdateMessage>
{
...
public async Task Consume(ConsumeContext<MappingUpdateMessage> context)
{
await Task.Run(async () =>
{
if (context.Message == null)
{
return;
}
...
});
}
}
// Message class (will have more properties in the future, thus not just using a string consumer)
public class MappingUpdateMessage
{
public string Message { get; set; }
}
Python:
import pika
import json
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='mappingQueue', exchange_type='fanout', durable=True)
message = {
"message" : {
"message": "Hello World"
},
"messageType": [
"urn:message:MassTransit.Tests:ValueMessage"
]
}
channel.basic_publish(exchange='mappingQueue',
routing_key='mappingQueue',
body=json.dumps(message))
connection.close()
print("sent")
For those with the same problem, I figured it out eventually:
..
config.ReceiveEndpoint("mappingQueue", e =>
{
e.ClearMessageDeserializers();
e.UseRawJsonSerializer();
e.ConfigureConsumer<MappingUpdateConsumer>(provider);
});
...
I wanted to apply the official django channels tutorial from https://channels.readthedocs.io/en/latest/tutorial/part_2.html to a simple android app.
At https://medium.com/#ssaurel/learn-to-use-websockets-on-android-with-okhttp-ba5f00aea988 I found a simple project but that uses the Echo WebSocket Server available at http://www.websocket.org/echo.html.
I copy-pasted the same project but replaced the Echo WebSocket Server with my own websocket server using django channels.
Here is the code:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button start;
private TextView output;
private OkHttpClient client;
private final class EchoWebSocketListener extends WebSocketListener {
private static final int NORMAL_CLOSURE_STATUS = 1000;
#Override
public void onOpen(WebSocket webSocket, Response response) {
Log.d(TAG, "onOpen() is called.");
JSONObject obj = new JSONObject();
JSONObject obj2 = new JSONObject();
try {
obj.put("message" , "Hello");
obj2.put("message", "Goodbye!");
} catch (JSONException e) {
e.printStackTrace();
}
webSocket.send(obj.toString());
//webSocket.send("What's up ?");
//webSocket.send(ByteString.decodeHex("deadbeef"));
webSocket.close(NORMAL_CLOSURE_STATUS, obj2.toString());
}
#Override
public void onMessage(WebSocket webSocket, String text) {
Log.d(TAG, "onMessage() for String is called.");
output("Receiving : " + text);
}
#Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
Log.d(TAG, "onMessage() for ByteString is called.");
output("Receiving bytes : " + bytes.hex());
}
#Override
public void onClosing(WebSocket webSocket, int code, String reason) {
Log.d(TAG, "onClosing() is called.");
webSocket.close(NORMAL_CLOSURE_STATUS, null);
output("Closing : " + code + " / " + reason);
}
#Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
Log.d(TAG, "onFailure() is called.");
output("Error : " + t.getMessage());
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
output = (TextView) findViewById(R.id.output);
client = new OkHttpClient();
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
start();
}
});
}
void start() {
Request request = new Request.Builder().url("ws://192.168.122.1:8080/ws/chat/lobby/").build();
EchoWebSocketListener listener = new EchoWebSocketListener();
WebSocket ws = client.newWebSocket(request, listener);
client.dispatcher().executorService().shutdown();
}
private void output(final String txt) {
runOnUiThread(new Runnable() {
#Override
public void run() {
output.setText(output.getText().toString() + "\n\n" + txt);
}
});
}
}
To send & receive messages to/from the server, they use WebSocketListener from the okhttp3 library.
My consumers.py file is the same as in the Django channels tutorial. More specifically, I used the same setup.
Nevertheless, to be on the same page, I post how my consumers.py file look like:
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
import json
class SignallingConsumer(WebsocketConsumer):
def connect(self):
print("connect() is called.")
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = 'chat_%s' % self.room_name
# Join room group
async_to_sync(self.channel_layer.group_add)(
self.room_group_name,
self.channel_name
)
self.accept()
def disconnect(self, close_code):
print("disconnect() is called.")
# Leave room group
async_to_sync(self.channel_layer.group_discard)(
self.room_group_name,
self.channel_name
)
# Receive message from WebSocket
def receive(self, text_data):
print("receive() is called with " + text_data)
text_data_json = json.loads(text_data)
message = text_data_json['message']
print("message contains: " + message)
# Send message to room group
async_to_sync(self.channel_layer.group_send)(
self.room_group_name,
{
'type': 'chat_message',
'message': message
}
)
# Receive message from room group
def chat_message(self, event):
print("the message from the event is: " + event['message'])
message = event['message']
# Send message to WebSocket
self.send(text_data=json.dumps({
'message': message
}))
I added some prints to understand better what is going under the hood and changed the name of the consumer since I want to use it later as a signalling server for another project when I understand the basics.
On the console, I get the following:
System check identified no issues (0 silenced).
July 25, 2019 - 10:34:24
Django version 2.2.3, using settings 'signalingserver.settings'
Starting ASGI/Channels version 2.2.0 development server at http://192.168.122.1:8080/
Quit the server with CONTROL-C.
WebSocket HANDSHAKING /ws/chat/lobby/ [192.168.122.1:54194]
connect() is called.
WebSocket CONNECT /ws/chat/lobby/ [192.168.122.1:54194]
receive() is called with {"message":"Hello"}
message contains: Hello
WebSocket DISCONNECT /ws/chat/lobby/ [192.168.122.1:54194]
the message from the event is: Hello
disconnect() is called.
My LogCat output is the following:
07-25 12:35:22.754 5297-5319/com.celik.abdullah.simplewebsocketproject D/MainActivity: onOpen() is called.
07-25 12:35:22.810 5297-5319/com.celik.abdullah.simplewebsocketproject D/MainActivity: onClosing() is called.
From these outputs I assume that the connection & sending part is functioning. But the receiving of data from the websocket server is not working because the onMessage() on my client side (android app) is not called.
How, I can fix that ?
Thank you in advance.
In case anyone else sees this, the issue is because you are closing the web socket in your onOpen method before you get a chance to receive anything back from the server. You should remove the line:
webSocket.close(NORMAL_CLOSURE_STATUS, obj2.toString());
And call close at a later time when you are no longer using the web socket connection.
Everything else looks great, and works as written.
How can I use a StreamSocket in a Windows 8 Metro app to connect to a Python+OpenSSL-based server using a self-signed certificate in the server ? I've tried adding the server's public key to the various trusted stores on the Windows Desktop to no avail. Everything I've tried yields the same results: an exception with the same message as in this post.
I'm developing with Visual Studio 2013 on Windows 8.1, connecting to a Python 3.4 server running OpenSSL 1.0.1h
I was able to find the answer. Microsoft provides a sample here. Once you download the sample, check out scenario 5. Based on the code in that scenario, here's a Windows 8 App Store unit test class that provides an example of how to use the code:
namespace Services.Implementation.Tests
{
using System;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Windows.Security.Cryptography.Certificates;
using System.Collections.Generic;
[TestClass]
public class NetworkConnectionIPv6Tests
{
#region Test Lifecycle Members
/// <summary>
/// Gets or sets the test context.
/// </summary>
/// <value>
/// The test context.
/// </value>
public TestContext TestContext
{
get; set;
}
#endregion // Test Lifecycle Members
#region Test Methods
[TestMethod, TestCategory("Integration")]
public void TestConnection()
{
const int ServerPort = 63253;
const string ServerIpAddress = "fe80::7ed1:c3ff:fed9:6fc7";
HostName hostName = new HostName(ServerIpAddress);
byte[] receiveBuffer = new byte[4096];
using (StreamSocket streamSocket = new StreamSocket())
{
bool retry = true;
do
{
try
{
streamSocket.ConnectAsync(hostName, ServerPort.ToString(), SocketProtectionLevel.Tls12).GetAwaiter().GetResult();
string certInformation = GetCertificateInformation(
streamSocket.Information.ServerCertificate,
streamSocket.Information.ServerIntermediateCertificates);
Debug.WriteLine("Certificate information: {0}", certInformation);
retry = false;
}
catch (Exception exception)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
// If the exception was caused by an SSL error that is ignorable we are going to prompt the user
// with an enumeration of the errors and ask for permission to ignore.
if (streamSocket.Information.ServerCertificateErrorSeverity != SocketSslErrorSeverity.Ignorable)
{
Debug.WriteLine("Connect failed with error: " + exception.Message);
Assert.Fail("Failed to avoid unignorable errors");
}
// ---------------------------------------------------------------------------
// WARNING: Only test applications may ignore SSL errors.
// In real applications, ignoring server certificate errors can lead to MITM
// attacks (while the connection is secure, the server is not authenticated).
// ---------------------------------------------------------------------------
streamSocket.Control.IgnorableServerCertificateErrors.Clear();
foreach (var ignorableError in streamSocket.Information.ServerCertificateErrors)
{
streamSocket.Control.IgnorableServerCertificateErrors.Add(ignorableError);
}
}
} while (retry);
byte[] messageBytes = Encoding.UTF8.GetBytes("Test");
Stopwatch stopwatch = Stopwatch.StartNew();
uint bytesSent = streamSocket.OutputStream.WriteAsync(messageBytes.AsBuffer()).GetAwaiter().GetResult();
Assert.AreEqual(messageBytes.Length, (int) bytesSent, "Failed to sent the correct amount of bytes");
IBuffer bytesReceived = streamSocket.InputStream.ReadAsync(receiveBuffer.AsBuffer(), (uint) receiveBuffer.Length, InputStreamOptions.None).GetAwaiter().GetResult();
stopwatch.Stop();
Debug.WriteLine("Remote call turnaround in {0} seconds", stopwatch.Elapsed.TotalSeconds);
Assert.IsTrue(bytesReceived.Length > 0, "There were no bytes received from the server");
string responseString = new string(Encoding.UTF8.GetChars(receiveBuffer, 0, (int) bytesReceived.Length));
Assert.AreEqual("Test right back", responseString, "Failed to receive the expected message from the server");
}
}
#endregion // Test Methods
#region Helper Methods
/// <summary>
/// Gets detailed certificate information
/// </summary>
/// <param name="serverCert">The server certificate</param>
/// <param name="intermediateCertificates">The server certificate chain</param>
/// <returns>A string containing certificate details</returns>
private string GetCertificateInformation(
Certificate serverCert,
IReadOnlyList<Certificate> intermediateCertificates)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("\tFriendly Name: " + serverCert.FriendlyName);
sb.AppendLine("\tSubject: " + serverCert.Subject);
sb.AppendLine("\tIssuer: " + serverCert.Issuer);
sb.AppendLine("\tValidity: " + serverCert.ValidFrom + " - " + serverCert.ValidTo);
// Enumerate the entire certificate chain.
if (intermediateCertificates.Count > 0)
{
sb.AppendLine("\tCertificate chain: ");
foreach (var cert in intermediateCertificates)
{
sb.AppendLine("\t\tIntermediate Certificate Subject: " + cert.Subject);
}
}
else
{
sb.AppendLine("\tNo certificates within the intermediate chain.");
}
return sb.ToString();
}
#endregion // Helper Methods
}
}
The key to using this example is that you have to be using Visual Studio 2013 targeting for Windows 8.1.